Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: json return value

by tobyink (Abbot)
on Jul 11, 2013 at 08:37 UTC ( #1043654=note: print w/ replies, xml ) Need Help??


in reply to json return value

You're not checking whether the response is an HTTP success...

my $req = POST( "http://myurl/public_html/cgi-bin/json-addition.cgi", Content_Type => 'application/json', Content => $json->encode($data), ); my $response = $ua->request($req); $response->is_success or die($response->status_line); my $result = $json->decode($response->decoded_content); print $result->{c}, "\n";
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: json return value
Download Code
Re^2: json return value
by vsespb (Hermit) on Jul 11, 2013 at 12:17 UTC
    I believe LWP can also return is_success=true + X-Died header https://github.com/libwww-perl/libwww-perl/issues/46 (but probably not related to OP code ), and also return truncated responses https://rt.cpan.org/Public/Bug/Display.html?id=85759
Re^2: json return value
by stenasc (Novice) on Jul 13, 2013 at 22:17 UTC

    I give up on this json/perl stuff. The cgi stuff looks as if it just responds to html pages not anything else and with not having permission to access cgi files, its just too difficult. All I want is to send an integer to a website from a standalone perl client using post and get an integer returned. Is it that difficult? Embedded development is easy compared to this. I'm beginning to lose my belief in this Perl religion !! I think I'll stick to the C sinners club.

      It's really not that difficult...

      Server

      use v5.14; use CGI (); use JSON::PP (); my $q = 'CGI'->new; my $json = 'JSON::PP'->new; 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 });

      Client

      use v5.14; use HTTP::Tiny (); use JSON::PP (); my $json = 'JSON::PP'->new; my $http = 'HTTP::Tiny'->new; my $url = 'http://buzzword.org.uk/2013/json-addition.cgi'; my $input = { a => 40, b => 2 }; my @request = ($url => { content => $json->encode($input) }); my $response = $http->post(@request); my $output = $json->decode($response->{content}); say $output->{c};

      (All modules used above are part of Perl core as of 5.14+, but CGI is scheduled to leave core in a future version of Perl.)

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

        Thank you very much. I greatly appreciate it. I get the correct value returned when I use the URL http://buzzword.org.uk/2013/json-addition.cgi but I get malformed JSON string, neither array, object, number, string or atom, at er offset 0 (before "<!DOCTYPE HTML PUBLI...") at json_client.pl line 12 when I use my own URL http://myurl.com/cgi-bin/json_mode.cgi. I have installed the server in the cgi-bin directory with permission 755 and the perl has valid syntax etc. It looks as if I cannot access the cgi file in the cgi-bin directory. Do you know a way around this?

        I also see it described as text/cgi whereas some, but not all of the other cgi files are described as application/cgi. Any ideas why this is the case?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-08-30 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls