Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Getting error while decoding JSON object via POST method

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


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

Here is an example of a CGI script that accepts JSON via HTTP POST, reads two numbers "a" and "b" from the input, and adds them together returning "c", plus an example client that uses the script...

#!/usr/bin/env perl use strict; use warnings; use CGI (); use JSON (); my $q = CGI->new; my $json = JSON->new->utf8; my $input = $json->decode( $q->param('POSTDATA') ); my $a = $input->{a}; my $b = $input->{b}; my $c = $a + $b; print $q->header("application/json"); print $json->encode({ c => $c });
#!/usr/bin/env perl use strict; use warnings; use JSON (); use LWP::UserAgent (); use HTTP::Request::Common qw( POST ); my $data = { a => 40, b => 2 }; my $json = JSON->new->utf8; my $ua = LWP::UserAgent->new; my $req = POST( "http://buzzword.org.uk/2013/json-addition.cgi", Content_Type => 'application/json', Content => $json->encode($data), ); my $result = $json->decode( $ua->request($req)->decoded_content ); print $result->{c}, "\n";
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: Getting error while decoding JSON object via POST method
Select or Download Code
Re^2: Getting error while decoding JSON object via POST method
by perlCrazy (Monk) on Feb 06, 2013 at 10:26 UTC
    thanks for response, still getting same error. i tried code : cleint :
    use strict; use warnings; use JSON (); use LWP::UserAgent (); use HTTP::Request::Common qw( POST ); use Data::Dumper; my $data = { a => 40, b => 2 }; my $json = JSON->new->utf8->allow_nonref; my $ua = LWP::UserAgent->new; my $req = POST( "$my_url", Content_Type => 'application/json', Content => $json->encode($data), ); my $response = $ua->request($req); if ($response->is_success) { #print Dumper($response->content()); my $perlData = $json->decode($response->content()); print Dumper($perlData); } else{ print " inside falied block\n"; }
    server side:
    #use cgi.pm to get json as input my $q = CGI->new; my $json = JSON->new->utf8; my $input = $json->decode( $q->param('POSTDATA') ); #my $input = $q->param('POSTDATA'); my $a = $input->{'a'}; my $b = $input->{'b'}; my $c = $a + $b; print $q->header("application/json"); #print Dumper($input); print $json->encode({ c => $c });
    output of run : ./test_client.pl $VAR1 = { 'error_msg' => 'Failed during execution: malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") ', 'status_code' => 500 }; Thanks
      And the reason you don't DUMP the data is because?
        which data ?
        just dumped the data server side and loos like :
        {"a":40,"b":2={"a":40,&#034
        it is broken.
Re^2: Getting error while decoding JSON object via POST method
by Anonymous Monk on May 19, 2014 at 16:21 UTC
    I'm getting this exact same error when trying to access the body of an application/json POST when the request is HTTP 1.1. If it's an HTTP 1.0 request, all is fine. HTTP 1.1 requests don't set the "Content-Length" header and use "Transfer-Encoding" with a value of "chunked". Does anyone know the proper way to grab the JSON payload of a POST request that will work with both HTTP 1.0 and HTTP 1.1?

      Implementing a transfer encoding on the server side is not easy. I would try to prevent sending data using the "chunked" transfer encoding. Depending on your client library, there are different ways to prevent that.

        I must be missing something here. At my $work, we use CGI::Application on the front end and simply decode_json( $self->query->param( 'POSTDATA' ) ) to get the values regardless of HTTP 1.0 or HTTP 1.1 (which is what we use.) We POST with a content type of "application/json".

        It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017369]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-11-27 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (190 votes), past polls