stenasc has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: json return value
by Corion (Patriarch) on Jul 11, 2013 at 08:38 UTC | |
|
Re: json return value
by tobyink (Canon) on Jul 11, 2013 at 08:37 UTC | |
by vsespb (Chaplain) on Jul 11, 2013 at 12:17 UTC | |
by stenasc (Novice) on Jul 13, 2013 at 22:17 UTC | |
by tobyink (Canon) on Jul 14, 2013 at 01:36 UTC | |
by stenasc (Novice) on Jul 14, 2013 at 22:27 UTC | |
by tobyink (Canon) on Jul 15, 2013 at 05:45 UTC | |
| |
by Anonymous Monk on Jul 14, 2013 at 23:44 UTC | |
|
Re: json return value
by marto (Cardinal) on Jul 11, 2013 at 08:42 UTC | |
by tobyink (Canon) on Jul 11, 2013 at 09:06 UTC | |
by marto (Cardinal) on Jul 11, 2013 at 10:07 UTC | |
by marto (Cardinal) on Jul 11, 2013 at 09:16 UTC | |
by Anonymous Monk on Jul 11, 2013 at 09:47 UTC | |
by marto (Cardinal) on Jul 11, 2013 at 09:53 UTC | |
| |
by stenasc (Novice) on Jul 12, 2013 at 10:18 UTC | |
by marto (Cardinal) on Jul 12, 2013 at 10:34 UTC | |
by stenasc (Novice) on Jul 13, 2013 at 20:56 UTC | |
| |
|
Re: json return value
by Anonymous Monk on Jul 11, 2013 at 08:43 UTC | |
by Anonymous Monk on Jul 11, 2013 at 08:57 UTC |