How to POST content with an HTTP Request (Perl)

放肆的年华 提交于 2019-12-04 11:18:25

问题


use LWP::UserAgent;
use Data::Dumper;

my $ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
my $req = new HTTP::Request POST => 'http://example.com';
$req->content('port=8', 'target=64'); #problem
my $res = $ua->request($req);

print Dumper($res->content);

How can I send multiple pieces of content using $req->content? What kind of data does $req->content expect?

It only sends the last one.

Edit:

Found out if i format it like 'port=8&target=64' it works. Is there a better way?


回答1:


my $ua      = LWP::UserAgent->new(); 
my $request = POST( $url, [ 'port' => 8, 'target' => 64 ] ); 
my $content = $ua->request($request)->as_string(); 



回答2:


The answer given didn't work for me. I still had the same problem as OP.

The documentation for LWP::UserAgent wants a hash or array reference.

This works:

my $url = 'https://www.google.com/recaptcha/api/siteverify';
my $ua      = LWP::UserAgent->new(); 

my %form;
$form{'secret'}='xxxxxxxxxxxxxxxxxxxxxxx';
$form{'response'}=$captchaResponse;

my $response = $ua->post( $url, \%form ); 
my $content = $response->as_string();


来源:https://stackoverflow.com/questions/11264470/how-to-post-content-with-an-http-request-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!