尝试从POST
脚本发送perl
请求时出错:
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->credentials($netloc,$realm,$username,$password);
use HTTP::Request::Common;
my $req = HTTP::Request::Common::POST($url,'content' => $conf);
$req->as_string()
是
POST http:.....
Content-Length: 31003
Content-Type: application/x-www-form-urlencoded
.....&createTime=1361370652541¶meters=HASH(0x28fd658)¶meters=HASH(0x28fd670)¶meters=HASH(0x28fd6e8)¶meters=HASH(0x28fd760)&nodes=32&.....&alerts=HASH(0x632d00)&alerts=HASH(0x245abd0)&.....
我得到的错误是
Unexpected character ('H' (code 72)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
这让我怀疑问题是重复的
parameters=HASH(...)
和alerts=HASH(...)
元素;
相反,我希望看到像
alerts=%5B%7B%22email%22%3A%22foo%40bar.com%22%2C%22type%22%3A%221%22%2C%22timeout%22%3A%22%22%7D%5D
$conf
是哈希引用,$conf->{"parameters"} and
$ conf-> {“alerts”}`是数组引用(其元素是哈希
参考文献)。
我做错了什么?
答案 0 :(得分:3)
你不能发表参考文献;你可能需要以某种方式序列化它们;服务器期待什么?
从错误看起来整个数组或者其中的每个哈希都应该用JSON序列化:
use JSON; # preferably have JSON::XS installed
my %prepared_conf = %$conf;
for my $field ( 'parameters', 'alerts' ) {
$prepared_conf{$field} =
JSON::to_json( $prepared_conf->{$field}, { 'ascii' => 1 } );
}
my $req = HTTP::Request::Common::POST($url,'content' => \%prepared_conf);