12
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?

Borodin
  • 126,100
  • 9
  • 70
  • 144
Takkun
  • 6,131
  • 16
  • 52
  • 69

3 Answers3

17
my $ua      = LWP::UserAgent->new(); 
my $request = POST( $url, [ 'port' => 8, 'target' => 64 ] ); 
my $content = $ua->request($request)->as_string(); 
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • 3
    Despite the code in the question I'd go with a full OOP approach: `my $ua = LWP::UserAgent->new(); my $response = $ua->post($url, $parameter); my $content = $response->as_string();` – dennis Aug 22 '13 at 10:09
4

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();
Community
  • 1
  • 1
Cole Tarbet
  • 160
  • 12
2

Using together LWP::UserAgent and HTTP::Request as it is also quite common if not even more frequent practice , I was little puzzled that the standard POST and GET / request were almost not discussed at SO aside from json as them are in vast majority used.

POST

    my $ua  = LWP::UserAgent->new();
    my $req = new HTTP::Request( 
       'POST' => "http://url/path", 
       ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'],
       'par1=par1value&par2=par2value'
    );
    $ua->request($req);

similarly for the GET

    my $ua  = LWP::UserAgent->new();
    my $req = new HTTP::Request( 
       'GET' => "http://url/path", 
       ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'],
       'par1=par1value&par2=par2value' # or I presume attaching the query string directly to the url
    );
    $ua->request($req);

another format form , where the first two parameters (method and url) are not fused into a one, not like the previous, but separately

my $request = HTTP::Request->new( 'POST', $url, [ parameter1 => 'parameter1Value' ] );
request->header( 'Content-Type' => 'application/json' )

There is a similar question, but just regards LWP and Json, but it could be probably accomplished only by using both LWP and HTTP::Request together as suggested by that question chosen answer, and the POST and GET were missing there but it might not have been obvious

How can I make a JSON POST request with LWP?

Note: I post this specially also, since the concrete/concise usage for POST/GET is not mentioned even in the documentation https://metacpan.org/pod/HTTP::Request

FantomX1
  • 1,577
  • 2
  • 15
  • 23