[英]LWP::UserAgent and HTTP::Request for a POST request
In a certain script I tried to write this: 在某个脚本中,我尝试编写以下代码:
my $ua = LWP::UserAgent->new;
my $res = $ua->post($url, Content => $data);
and got "400 Bad Request". 并收到“ 400错误请求”。 After some reading I tried this: 经过一番阅读,我尝试了一下:
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( 'POST', $url );
$req->content( $data );
my $res = $ua->request( $req );
and it worked, but I thought these two should do the same. 并且有效,但是我认为这两个应该做的一样。 What am I missing here? 我在这里想念什么? Am I misunderstanding something in the documentation of HTTP::Request and LWP::UserAgent ? 我是否误解了HTTP :: Request和LWP :: UserAgent文档中的某些内容?
Is there a way to ask LWP::UserAgent to print what it is doing? 有没有办法让LWP :: UserAgent打印它在做什么?
Here's one way to do it: 这是一种实现方法:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
{
no strict "refs";
no warnings "redefine";
my $orig_sub = \&LWP::UserAgent::send_request;
*{"LWP::UserAgent::send_request"} = sub {
my ($self, $request) = @_;
print $request->as_string . "\n";
my $response = $orig_sub->(@_);
print $response->as_string . "\n";
return $response;
};
}
my $a = LWP::UserAgent->new;
my $response = $a->get("http://google.com");
It will print out all the requests and responses that LWP::UserAgent does. 它将打印出LWP :: UserAgent所做的所有请求和响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.