Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Keep It Simple, Stupid
 
PerlMonks  

Re^2: json return value

by stenasc (Novice)
on Jul 13, 2013 at 22:17 UTC ( #1044173=note: print w/ replies, xml ) Need Help??


in reply to Re: json return value
in thread json return value

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.


Comment on Re^2: json return value
Re^3: json return value
by tobyink (Abbot) on Jul 14, 2013 at 01:36 UTC

    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?

        Have you visited http://myurl.com/cgi-bin/json_mode.cgi in your browser?

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-04-19 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (473 votes), past polls