How to POST content with an HTTP Request (Perl)

Takkun picture Takkun · Jun 29, 2012 · Viewed 41.7k times · Source
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?

Answer

Ωmega picture Ωmega · Jun 29, 2012
my $ua      = LWP::UserAgent->new(); 
my $request = POST( $url, [ 'port' => 8, 'target' => 64 ] ); 
my $content = $ua->request($request)->as_string();