Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Getting error while decoding JSON object via POST method

by tobyink (Abbot)
on Feb 06, 2013 at 06:39 UTC ( #1017344=note: print w/replies, xml ) Need Help??


in reply to Getting error while decoding JSON object via POST method

The error is on these two lines.

my $jsonText = $q->param('inputdata'); my $outputDataRef = $json->decode($inputData);

If you'd used strict Perl would have told you about the undeclared variable $inputData.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Getting error while decoding JSON object via POST method
by perlCrazy (Monk) on Feb 06, 2013 at 07:20 UTC
    Sorry for not providing the complete code. updated the code just now. problem is not with variable definition, please check the error:
    malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)"

      Have you printed out your JSON data string at the receiving end to see what you get there? An empty string gives the same error message:

      >perl -MJSON -e "print JSON->new->decode('')" malformed JSON string, neither array, object, number, string or atom, +at character offset 0 (before "(end of string)") at -e line 1.

      I think you are trying to pass the data around in a very bad manner:

      <c> "/projectname/restfulapi?inputdata=$jsonData"; <c>

      If you send POST data, please read HTTP::Request on how to send POST data. If you want to send the data in an URI, please encode it properly using URI::Encode.

        mo luck!! tried like this:
        my $inData = { "name" => "test", "id" => "test", "test" => [1,2,3,4],} +; my $json = JSON->new->utf8->allow_nonref; my $jsonData = $json->encode($inData); print Dumper($jsonData); ##Output of dumper : $VAR1 = '{"test":[1,2,3,4],"name":"test","id":"test"}'; # post set post request my $req = HTTP::Request->new('POST', $rest_url); $req->content($jsonData); my $response = $iua->request($req);
        getting same error.

      This error message occurs when you pass the empty string or an undefined variable to be decoded by JSON, JSON::PP or JSON::XS.

      So I'd suggest that even with the updated code, the problem is related to the initialization of $jsonText - it appears to be undef.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017344]
help
Chatterbox?
[Corion]: Oh yay. $project has faffed around for 2 years and now pushed a hasty 2 weeks solution into production without telling me. Of course, the new data also needs changes on my side to be processed correctly. Now they try to raise incidents against my ...
[Corion]: ... input systems to make the change outside of the organized project because I don't have ressources for the project to make the change.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-08-17 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (285 votes). Check out past polls.

    Notices?