Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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

Replies are listed 'Best First'.
Re^2: json return value
by vsespb (Chaplain) on Jul 11, 2013 at 12:17 UTC
    I believe LWP can also return is_success=true + X-Died header (but probably not related to OP code ), and also return truncated responses
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...


      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 });


      use v5.14; use HTTP::Tiny (); use JSON::PP (); my $json = 'JSON::PP'->new; my $http = 'HTTP::Tiny'->new; my $url = ''; 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 but I get malformed JSON string, neither array, object, number, string or atom, at er offset 0 (before "<!DOCTYPE HTML PUBLI...") at line 12 when I use my own URL 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043654]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-04-21 12:12 GMT
Find Nodes?
    Voting Booth?