【发布时间】:2015-02-21 04:40:46
【问题描述】:
我正在尝试使用 Perl LWP 库向 REST Web 服务发送 POST 请求。我能够成功完成 POST 请求而没有错误;但是,当我尝试获取响应内容时,它返回一个空序列[]。我已经使用 Chrome 中的 Postman 应用程序向 Web 服务发送了 POST 请求,它返回了预期的响应。似乎这只发生在 POST 和 PUT 请求中:GET 请求返回预期的内容。
这是我的 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";
}
我做错了吗?
【问题讨论】:
-
您应该将使用脚本发送的内容与使用浏览器发送的内容进行比较!
标签: perl lwp-useragent