nijin has asked for the wisdom of the Perl Monks concerning the following question:

I am having an issue with Json RPC and mod_perl. I am trying to return a value from a cgi script which is running in Apache through mod_perl. But in the return value, following apache headers are automatically added and therefore I am not able to access the return value from my client script.
Status: 200 Content-Type: application/json; charset=UTF-8
In my Apache configuration file I have following directives.
LoadModule perl_module modules/mod_perl.so PerlSwitches -w PerlSwitches -T Alias /perl /var/www/html/perl <Directory /var/www/html/perl> SetHandler perl-script PerlResponseHandler ModPerl::Registry Options +ExecCGI </Directory>
My cgi script is pasted below.
#!/usr/bin/perl use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use JSON::RPC::Server::CGI; use strict; use Data::Dumper; my $server = JSON::RPC::Server::CGI->new; $server->dispatch('Myapp')->handle(); The Myapp.pm is #!/usr/bin/perl package Myapp; use base qw(JSON::RPC::Procedure); # Perl 5.6 or more than use strict; use Data::Dumper; sub test : Public(u1:str){ my ($s, $obj) = @_; my $u1 = $obj->{u1}; return $u1; } 1;
My client side script is
#!/usr/bin/perl use JSON::RPC::Client; use Data::Dumper; my $client = new JSON::RPC::Client; my $uri = 'http://IP/perl/test.cgi'; $client->prepare($uri, ['test']); $str= $client->test('testing'); print "$str\n\n";
In normal case the output should be testing . But in my case I am getting the below error.
malformed JSON string, neither array, object, number, string or atom, +at character offset 0 (before "Status: 200\r\nConte...") at /usr/loca +l/share/perl5/JSON/RPC/Client.pm line 186
The issue is because some http headers are automatically get added to the return value. Is there any way to suppress these headers? Note: Kindly don't recommend normal cgi scripts or running perl script as daemon because it is already working and tested from my end. We are using mod_perl for high performance.

Replies are listed 'Best First'.
Re: Issue in Json RPC with mod_perl
by Corion (Patriarch) on Jul 04, 2014 at 13:51 UTC

    You must be using some weird version of JSON::RPC::Client. CPAN does not know of it (JSON::RPC::Client) and metacpan also does not know about it ([mcpan://JSON::RPC::Client]). The closest I can find is JSON::RPC::Legacy::Client, but that one uses LWP::UserAgent for the HTTP transfer, and LWP::UserAgent knows about headers.

    Please post the version number of the module and ideally also where you found it:

    perl -MJSON::RPC::Client -e 'print $JSON::RPC::Client::VERSION'
Re: Issue in Json RPC with mod_perl
by hippo (Archbishop) on Jul 04, 2014 at 13:29 UTC

    Have you enabled ParseHeaders? I think that should do the trick.

      Thank you for the reply. But ParseHeaders is already in disabled stage.

        Perhaps I have not been clear enough. If you have not enabled ParseHeaders already, then you should. It is for precisely the scenario where the script sends its own headers that ParseHeaders should be used. HTH.

        Update: FTAOD you should use

        PerlOptions +ParseHeaders