Hi,
getting this error while decoding the josn object via post method. surprosingly it works fine when GET method is used.
client code:
use Data::Dumper;
use JSON;
use URI::Escape;
use HTTP::Request::Common;
use HTTP::Response;
use LWP::UserAgent;
my $rest_url = "some url";
my $inData =
{ "name" => "test",
"id" => "test",
"test" => [1,2,3,4]
};
my $json = JSON->new->utf8->allow_nonref;
my $jsonData = $json->encode($inData);
my $apiCmd = "/projectname/restfulapi?inputdata=$jsonData";
my $iua = LWP::UserAgent->new;
#my $response = $iua->request(GET $rest_url.$apiCmd);
my $response = $iua->request(POST $rest_url.$apiCmd);
if ($response->is_success) {
#print Dumper($response->content());
my $perlData = $json->decode($response->content());
print Dumper($perlData);
}
else{
print " inside falied block\n";
}
api code:
use strict;
use Readonly;
use CGI;
use Data::Dumper;
use JSON;
#use cgi.pm to get json as input
my $q = CGI->new;
my $jsonText = $q->param('inputdata');
my $json = JSON->new->utf8->allow_nonref(1);
my $outputDataRef = $json->decode($jsonText);
print qq{Content-type: application/json; charset=utf8\n\n};
my $outputData = $json->encode($outputDataRef);
print $outputData;
It fails at api side while decoding. error details:
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)")
Any help will be greatly appreciated, Thanks.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.