Search code examples
perlcgiformslwp-useragent

Parse HTTP:Response object


I am having some difficulties getting results from a form via Perl. I believe I have successfully found the form and submitted the value I want to the appropriate field, but am unsure of how to turn the response object into something useful (If I print it out it shows up as the following).

HTTP::Request=HASH(0x895b8ac)

Here is the relevant code (assume $url is correct)


    my $ua = LWP::UserAgent->new;
    my $responce = $ua->get($url);
    my @form = HTML::Form->parse($responce);
    my $chosen = $form[0];
    $chosen->value('netid', $user);
    my $ro = $chosen->click('Search');

What can I do to make $ro useful?

Thanks!


Solution

  • To quote the HTML::Form docs on click:

    The result of clicking is an HTTP::Request object that can then be passed to LWP::UserAgent if you want to obtain the server response.

    So you can do:

    my $ua = LWP::UserAgent->new;
    my $response = $ua->get($url);
    my @form = HTML::Form->parse($response);
    my $chosen = $form[0];
    $chosen->value('netid', $user);
    my $ro = $chosen->click('Search');
    
    # If you want to see what you're sending to the server:
    print $ro->as_string;
    
    # Fetch the server's response:
    $response = $ua->request($ro);
    

    What you do with $response next depends on what you're trying to do.

    P.S. "responce" is usually spelled without a C. But HTTP does have a history of misspellings. (I'm looking at you, "Referer".)