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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting error while decoding JSON object via POST method
by perlCrazy (Monk) on Feb 06, 2013 at 10:26 UTC | |
by Anonymous Monk on Feb 06, 2013 at 10:35 UTC | |
by perlCrazy (Monk) on Feb 06, 2013 at 14:19 UTC | |
by perlCrazy (Monk) on Feb 06, 2013 at 10:53 UTC | |
|
Re^2: Getting error while decoding JSON object via POST method
by Anonymous Monk on May 19, 2014 at 16:21 UTC | |
by Corion (Patriarch) on May 19, 2014 at 17:43 UTC | |
by boftx (Deacon) on May 19, 2014 at 18:56 UTC | |
by Corion (Patriarch) on May 19, 2014 at 19:27 UTC |