First of all, this is my first post but I have been reading around here first trying to find a solution. Secondly, I haven't a clue about json. I'm an embedded C programmer with limited perl experience. Basically I want to send a simple string of json containing two parameters using a post command to a website and get an integer response. I used the following code that I found on this site as the server saved as a cgi file in the cgi-bin directory of the web server. The webserver is Apache. Do I need to install anything on the webserver to handle json or is it handled by default?
#!/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 });
The following, I used as client...
#!/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://myurl/public_html/cgi-bin/json-addition.cgi", Content_Type => 'application/json', Content => $json->encode($data), ); my $result = $json->decode( $ua->request($req)->decoded_content ); print $result->{c}, "\n";
I get the following response malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "Can't connect to myurl...") at json-client.pl line 21. As you can see fellow monks, I am an ignorant soul and need your salvation. Please enlighten me with some of your wisdom?
In reply to json return value by stenasc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |