Search code examples
perllwp-useragent

Having problem setting parameters for LWP::UserAgent


my %parameters = (
                        key => 'value'
                 );
my $response = $ua->get('http://example.com/i', %parameters);

I'm trying to get content of http://example.com/i?key=value,but after debugging I found the %parameters are stored in http headers instead of url parameters.

What's wrong in my code?

Though perldoc tells me that :

$ua->get( $url , $field_name => $value, ... )

But it should also work if I put those parameters in a %parameters,right?


Solution

  • The additional parameters to get are HTTP headers. For GET requests, arguments are included in the URL itself, URLencoded. You can use the URI module to create the appropriate URLs including GET variables, or construct them yourself (probably using URI::Escape to urlencode the values).

    e.g.:

    my %parameters = (
                            key => 'value'
                     );
    my $url = URI->new("http://example.com/i");
    $url->query_form(%parameters);
    my $response = $ua->get($url);