I was given the requirement not to use JSON :: RPC :: Client, instead use LWP to make calls.
Here is my code:
Server:
#!/usr/bin/perl use strict; use lib "."; use ServerLib; use JSON::RPC::Server::Daemon; die "Do Not run as Root\n" if($< == 0); print "Starting Daemon\n"; my $daemon = JSON::RPC::Server::Daemon->new(LocalPort => 8000); $daemon->dispatch({'/jsonrpc/API' => 'ServerLib'})->handle(); exit;
Module:
package ServerLib; use base qw(JSON::RPC::Procedure);
Working client:
#!/usr/bin/perl use strict; use JSON::RPC::Client; use Data::Dumper; my $client = new JSON::RPC::Client; my $uri = 'http://localhost:8000/jsonrpc/API'; my $obj = { method => 'echo',
Client does not work:
#!/usr/bin/perl -w use strict; use JSON; use LWP::Simple; use Data::Dumper; my $actionurl = "http://localhost:8000/jsonrpc/API"; my $ua = LWP::UserAgent->new(); $ua->agent("JSONClient/0.1"); my $object = { method => "echo", params => [@ARGV ] }; my $json = to_json($object); print "$json\n"; my $req = HTTP::Request->new(POST => $actionurl); $req->content_type('application/json'); $req->content($json); my $res = $ua->request($req); if ($res->is_success) { print "Succeeded:\n" . Dumper($res->dump); print "DUmp: ". $res->dump; my $result = to_json($res->content); } else { print "Failed\n"; }
I see a server process for both clients, but the second client has no data returned to it.
DUmp: HTTP/1.1 200 OK Connection: close Date: Tue, 03 Jan 2012 18:24:24 GMT Server: libwww-perl-daemon/5.827 Content-Type: application/json; charset=UTF-8 Client-Date: Tue, 03 Jan 2012 18:24:24 GMT Client-Peer: 127.0.0.1:8000 Client-Response-Num: 1 (no content)
Does anyone see what I am missing? It should be pretty simple, but for some reason I cannot find an answer string like RPC :: Client.
source share