Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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.

Replies are listed 'Best First'.
Re^3: json return value
by tobyink (Abbot) on Jul 14, 2013 at 01:36 UTC

    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?

        Have you visited in your browser?

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044173]
[choroba]: Yes,
[choroba]: depends on your DNS

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-05-21 12:02 GMT
Find Nodes?
    Voting Booth?