vsevilla has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to run the following code:
use WWW::Mechanize; use HTML::Form; use strict; my ( $content, $mech, $pass, $url, $user ); $user="user\@test.com"; $pass="mypass"; $url = "https://www.test.com". $mech = WWW::Mechanize->new(); push @{ $mech->requests_redirectable }, 'POST'; $mech->redirect_ok(); # get main page. $mech->get($url); die "ERROR(mech): ", $mech->response->status_line unless $mech->succes +s; # fill form with userid/pass and submit my $resp = $mech->submit_form( form_name => 'Login', fields => { USER => "$user", PASSWORD => "$pass" }, ); die "ERROR(resp): ", $resp->response->status_line unless $resp->is_suc +cess; # print output $content = $resp->content; die "ERROR can not get content: ", $mech->status_line unless $resp->is +_success; print $content; exit;
The idea is go get to the main page, fill out form with userid and password, and submit it. But, I get redirected instead, output is:
<HTML> <!-- File: redirectmeta.html --> <HEAD> <TITLE>Livelink - Redirection</TITLE> <META HTTP-EQUIV="Refresh" CONTENT="0; URL=/livelink815/livelink.exe?R +edirect=1"> </HEAD> </HTML> <!-- End File: redirectmeta.html -->
Please help, thanks.

Replies are listed 'Best First'.
Re: WWW::Mechanize form gets redirected
by mpeters (Chaplain) on Dec 13, 2004 at 00:47 UTC
    It's quite possible that the script you are submitting to on that server is returning a redirect to you. That's very common with login screens since they usually give you a cookie before you go to the next page so that you're logged-in status will be noted. Try checking for a cookie (just out of curiosity mainly :) from that response:
    my $header = $response->header('Cookie');
    Make sure that you have redirects turned on for mech, so that your agent will automatically follow the redirect. See the redirect_ok() method.

    If mech won't automatically follow a meta refresh redirect, then you could try parsing the response with something like HTML::Parser, then just submit to the new url (and make sure you use the same agent with a cookie_jar if you were given a cookie from the previous submition). HTH
      introducing $mech->redirect_OK(); gets error LWP::UserAgent::new: () Can't call method "request" on an undefined value even though LWP is there !