0

I have to make POST request to a URL which also contains GET variables (query string).

I tried the following (which looks like a most simepl/logical way) but it does not work:

my $ua = LWP::UserAgent->new;
my $res = $ua->post('http://my.domain/index.pl?login=yes', {
    username => $username, 
    password => $password
});

my.domain/index.pl does receive any requests but as soon as I remove query string "?login=yes" request is working correctly.

storyteller
  • 47
  • 1
  • 5
  • 1
    what does "it does not work" *mean*? what are you expecting to happen that doesn't or not expecting to happen that does? – ysth Feb 22 '13 at 01:45
  • @ysth I thought that appliaction did not answer at all. But I missed an "exit" command in an included file which was executed before my work. – storyteller Feb 22 '13 at 08:06
  • If you're doing more than just fetching the page, and you're wanting to follow links, or parse HTML to find images or links, take a look at WWW::Mechanize. http://search.cpan.org/dist/WWW-Mechanize It is a subclass of LWP::UserAgent, so anything you can do with LWP::UserAgent you can do with Mech, but Mech makes it much easier to do common tasks. – Andy Lester Feb 22 '13 at 23:03

1 Answers1

2
my $res = $ua->post('http://my.domain/index.pl?login=yes', {
    username => $username, 
    password => $password
});

boils down to

use HTTP::Request::Common qw( POST );

my $req = POST('http://my.domain/index.pl?login=yes', {
    username => $username, 
    password => $password,
});

my $res = $ua->request($req);

By using print $req->as_string();, you can see that does exactly what you said it should do.

POST http://my.domain/index.pl?login=yes
Content-Length: 35
Content-Type: application/x-www-form-urlencoded

password=PASSWORD&username=USERNAME

The problem is elsewhere.

ikegami
  • 367,544
  • 15
  • 269
  • 518