运行Perl脚本时显示错误。它说,内部服务器错误。错误代码500.我该如何处理?
use warnings;
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $url = "https://myhost/test/server.php";
my $req = HTTP::Request->new(POST => $url);
$req->header('content-type' => 'application/json');
my $post_data = {"value": "sample", "value2":"sample2"};
$req->content($post_data);
#print $req->as_string;
my $resp = $ua->request($req);
if ($resp->is_success) {
my $message = $resp->decoded_content;
print "Received reply: $message\n";
}
else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
}
答案 0 :(得分:0)
这不是perl:
my $post_data = {"value": "sample", "value2":"sample2"};
您需要将值括在单引号中:
my $post_data = '{"value": "sample", "value2":"sample2"}';
或者,您可以从perl哈希开始,并使用JSON
构建json字符串:
use JSON;
my %hash = ( value => 'sample', value2 => 'sample2' );
my $post_data = to_json( \%hash );