gpt4 book ai didi

Perl JSON::RPC::Client 使用 LWP::Agent

转载 作者:行者123 更新时间:2023-12-04 22:50:17 25 4
gpt4 key购买 nike

我被要求不使用 JSON::RPC::Client,而是使用 LWP 进行调用。

这是我的代码:

服务器:

#!/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;

模块:
package ServerLib;

use base qw(JSON::RPC::Procedure); # Perl 5.6 or more than
use JSON;
use Data::Dumper;
use Sys::Hostname;
use Net::RabbitMQ;
use YAML qw( LoadFile );
use strict;

$| = 1;
my $VERSION = 0.2.0;

sub echo : Public(message:string) {
my ($s, $obj) = @_;
my $message = $obj->{'message'};
print "got message => $message\n";
print "returning: $message\n";
my $object = { message => $message,
foobar => "garbage" };

return $object;
}
package ServerLib::system;

sub describe {
{
sdversion => "0.1",
name => 'ServerLib',
};
}

1;

工作客户:
#!/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', # or 'MyApp.sum'
params => ["my message"],
};

my $res = $client->call( $uri, $obj );

if($res){
if ($res->is_error) {
print "Error : ", $res->error_message;
}
else {
print Dumper($res->result);
}
}
else {
print $client->status_line;
}

非工作客户:
#!/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";
}

我看到两个客户端的服务器进程,但第二个客户端没有返回数据。
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)

有人看到我错过了什么吗?它应该很简单,但由于某种原因,我找不到像 RPC::Client 看到的响应字符串。

最佳答案

这是一个棘手的问题。我的一位同事发现了它,但他不在 Stackoverflow 上,所以我将发布修复程序。

这:

my $object = { method => "echo",
params => [@ARGV ] };

需要的版本=>“1.1”,添加到它成为:
my $object = { method => "echo",
params => [@ARGV],
version => "1.1",};

不知道为什么这很重要,但一旦添加它,它就像一个魅力。

关于Perl JSON::RPC::Client 使用 LWP::Agent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8716904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com