作为perl编程的新手,我仍然遇到麻烦。我想问一下如何在网址中发布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";
}
答案 0 :(得分:0)
你的JSON无效,因为你有一对额外的大括号。
如果您不确定如何格式化数据,请使用JSON
模块为您执行此操作:
use strict;
use warnings;
use JSON;
my %hash = (
value1 => "THEI3435J",
value2 => "3453fdf",
value3 => "Sep452",
);
print to_json( \%hash );
输出:
{"value1":"THEI3435J","value3":"Sep452","value2":"3453fdf"}