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?

0

3 Answers 3

16
my $ua      = LWP::UserAgent->new(); 
my $request = POST( $url, [ 'port' => 8, 'target' => 64 ] ); 
my $content = $ua->request($request)->as_string(); 
Sign up to request clarification or add additional context in comments.

1 Comment

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();
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();

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.