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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.