Search code examples
perlpostgetlwplwp-useragent

Sending POST request with GET variables in URL (with LWP::UserAgent)


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.


Solution

  • 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.