Re: Posting to a form on the web

Gisle Aas (gisle@aas.no)
25 Mar 1999 22:19:56 +0100


Rick Tuinenburg <rtuinenburg@csg-i.com> writes:

> I am using the following code to do this:
> 
>   # Create a user agent object
>   $ua = new LWP::UserAgent;
>   $ua->agent("AgentName/0.1 " . $ua->agent);
> 
>   # Create a request
>   my $req = new HTTP::Request POST => "$URL";
>   $req->content_type('application/x-www-form-urlencoded');
>   $req->content("$var1=$val1&$var2=$val2");
> 
> print "hello - ($SearchURL)";
>   # Pass request to the user agent and get a response back
>   my $res = $ua->request($req);
> 
>   # Check the outcome of the response
>       print $res->content;
> 
> This form on the web passes me another url which actually gives the
> result, so this little prog above says the following:
> 
> Object Moved
> 
> This object may be found here.
> 
> How do I make it go their automatically... I want the results...

The easiest way is probably to make a LWP::UserAgent subclass that
overrides the redirect_ok method so it returns TRUE for POST requests.
The default method look like this:

sub redirect_ok
{
    # draft-ietf-http-v10-spec-02.ps from www.ics.uci.edu, specify:
    #
    # If the 30[12] status code is received in response to a request using
    # the POST method, the user agent must not automatically redirect the
    # request unless it can be confirmed by the user, since this might change
    # the conditions under which the request was issued.

    my($self, $request) = @_;
    return 0 if $request->method eq "POST";
    1;
}

-- 
Gisle Aas