【发布时间】:2014-11-11 05:27:20
【问题描述】:
作为 perl 编程的新手,我仍然遇到问题。我想问一下如何在url中发布json数据并在浏览器中显示结果。我尝试了脚本(如下),但没有任何反应;在浏览器中看不到数据。我搜索了一些要测试的代码,但不幸的是我没有找到任何代码。我希望有人可以告诉我,如果我必须做一些代码。我很确定有。
use warnings;
use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
my $ua = LWP::UserAgent->new;
my $server_endpoint = 'https://..';
my $req = HTTP::Request->new( POST => $server_endpoint );
$req->header( 'content-type' => 'application/json' );
my $data = '{
{"value1" : "THEI3435J",
"value2" : "3453fdf",
"value3" : "Sep452"
}
} ';
$request->content($data);
my $resp = $ua->request($request);
print $request->as_string;
if ( $resp->is_success ) {
my $message = $resp->decoded_content;
print "$message\n";
} else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
}
【问题讨论】:
-
您的 JSON 无效。你有
{ { "key" : "value" } }这是错误的。你只需要一对花括号。 Perl 代码对我来说看起来不错。我假设“在浏览器中”是指当您使用该脚本发布到服务器后访问服务器以查看内容,对吧?