带有空序列响应的 Perl LWP Post
Perl LWP Post With Empty Sequence Response
我正在尝试使用 Perl LWP 库向 REST 网络服务发送 POST 请求。我能够毫无错误地成功完成 POST 请求;然而,当我尝试获取响应内容时,它 return 是一个空序列 []
。我已经在 Chrome 中使用 Postman 应用程序向 Web 服务发送了 POST 请求,并且它 return 是预期的响应。这似乎只发生在 POST 和 PUT 请求中:GET 请求 return 预期的内容。
这是我的 Perl 代码:
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use JSON;
use Data::Dumper;
my $url = "http://localhost:53076/api/excel(07375ebd-e21f-4ce4-91d7-49dc2de7ceb1)";
my $ua = LWP::UserAgent->new( keep_alive => 1 );
my $json = JSON->new->utf8->allow_nonref;
my @inputs = ( ( "key" => "A", "Value" => "12" ), ( "key" => "B", "Value" => "12" ) );
my @outputs = ( ( "key" => "A", "Value" => "12" ), ( "key" => "B", "Value" => "12" ) );
my %body;
$body{"Inputs"} = \@inputs;
$body{"Outputs"} = \@outputs;
my $jsonString = $json->encode(\%body);
my $response = $ua->post($url, "Content-Type" => "application/json; charset=utf-8", "Content" => $jsonString);
if ($response->is_success)
{
print "\n========== REQUEST CONTENT ==========\n";
print $response->decoded_content(), "\n";
}
else
{
print "\n========== ERROR ==========\n";
print "Error: ", $response->status_line(), "\n";
print $response->headers()->as_string(), "\n";
}
我是不是做错了什么?
在表达式中,()
除了影响优先级外什么都不做。例如,4 * ( 5 + 6 )
不同于 4 * 5 + 6
。因此,
my @inputs = ( ( key => "A", Value => "12" ), ( key => "B", Value => "12" ) );
只是一种奇怪的写法
my @inputs = ( "key", "A", "Value", "12", "key", "B", "Value", "12" );
如果您想创建一个散列和 return 对它的引用,使用 {}
即可完成。
my @inputs = ( { key => "A", Value => "12" }, { key => "B", Value => "12" } );
my @outputs = ( { key => "A", Value => "12" }, { key => "B", Value => "12" } );
这基本上是
的缩写
my %anon_i1 = ( key => "A", Value => "12" );
my %anon_i2 = ( key => "B", Value => "12" );
my @inputs = ( \%anon_i1, \%anon_i2 );
my %anon_o1 = ( key => "A", Value => "12" );
my %anon_o2 = ( key => "B", Value => "12" );
my @outputs = ( \%anon_o1, \%anon_o2 );
我正在尝试使用 Perl LWP 库向 REST 网络服务发送 POST 请求。我能够毫无错误地成功完成 POST 请求;然而,当我尝试获取响应内容时,它 return 是一个空序列 []
。我已经在 Chrome 中使用 Postman 应用程序向 Web 服务发送了 POST 请求,并且它 return 是预期的响应。这似乎只发生在 POST 和 PUT 请求中:GET 请求 return 预期的内容。
这是我的 Perl 代码:
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use JSON;
use Data::Dumper;
my $url = "http://localhost:53076/api/excel(07375ebd-e21f-4ce4-91d7-49dc2de7ceb1)";
my $ua = LWP::UserAgent->new( keep_alive => 1 );
my $json = JSON->new->utf8->allow_nonref;
my @inputs = ( ( "key" => "A", "Value" => "12" ), ( "key" => "B", "Value" => "12" ) );
my @outputs = ( ( "key" => "A", "Value" => "12" ), ( "key" => "B", "Value" => "12" ) );
my %body;
$body{"Inputs"} = \@inputs;
$body{"Outputs"} = \@outputs;
my $jsonString = $json->encode(\%body);
my $response = $ua->post($url, "Content-Type" => "application/json; charset=utf-8", "Content" => $jsonString);
if ($response->is_success)
{
print "\n========== REQUEST CONTENT ==========\n";
print $response->decoded_content(), "\n";
}
else
{
print "\n========== ERROR ==========\n";
print "Error: ", $response->status_line(), "\n";
print $response->headers()->as_string(), "\n";
}
我是不是做错了什么?
在表达式中,()
除了影响优先级外什么都不做。例如,4 * ( 5 + 6 )
不同于 4 * 5 + 6
。因此,
my @inputs = ( ( key => "A", Value => "12" ), ( key => "B", Value => "12" ) );
只是一种奇怪的写法
my @inputs = ( "key", "A", "Value", "12", "key", "B", "Value", "12" );
如果您想创建一个散列和 return 对它的引用,使用 {}
即可完成。
my @inputs = ( { key => "A", Value => "12" }, { key => "B", Value => "12" } );
my @outputs = ( { key => "A", Value => "12" }, { key => "B", Value => "12" } );
这基本上是
的缩写my %anon_i1 = ( key => "A", Value => "12" );
my %anon_i2 = ( key => "B", Value => "12" );
my @inputs = ( \%anon_i1, \%anon_i2 );
my %anon_o1 = ( key => "A", Value => "12" );
my %anon_o2 = ( key => "B", Value => "12" );
my @outputs = ( \%anon_o1, \%anon_o2 );