Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

by gods
on Feb 11, 2000 at 00:06 UTC ( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Getting error while decoding JSON object via POST method by perlCrazy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2016-09-25 00:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Extraterrestrials haven't visited the Earth yet because:







      Results (462 votes). Check out past polls.