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

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

Replies are listed 'Best First'.
Re^4: json return value
by stenasc (Novice) on Jul 14, 2013 at 22:27 UTC

    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

        Yes...I get the following. It looks as if I don't have permission to contact the website.

        Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@myurl.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

        Yes...it gives me this..

        Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@myurl.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

        However if I have a small cgi like the following, it works fine when accessed through the browser

        #!/usr/bin/perl print "Content-type:text/html\r\n\r\n"; print '<html>'; print '<head>'; print '<title>Hello Word</title>'; print '</head>'; print '<body>'; print '<h2>Hello World</h2>'; print '</body>'; print '</html>';

        I've been in contact with the server administrator and he sent me the log file results.

        Mon Jul 15 14:49:20 2013 error client 87.194.165.154 Premature end of script headers: json_mode.cgi

        He also suggested that I add the following to the code, but that is already in the code He also told me that it was because the script wasn't outputting valid html headers.

        print("Content-type: text/json"); or print("Content-type:application/json");

        The client is working fine because I can get the correct response from http://buzzword.org.uk/2013/json-addition.cgi'