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

Hi,

I've written this quick script which works until I put in the redirect_ok call to get it to follow a 302.

use WWW::Mechanize; use CGI; my $agent=WWW::Mechanize->new(); $agent->add_header( Cookie => $ENV{'HTTP_COOKIE'} ); $agent->get('http://lhrbod-apdhz001.ad.com/ess/SignUpAsEmployee.aspx') +; $agent->redirect_ok(); print CGI::header(); print $agent->content();

The logs say Can't call method "value" on an undefined value at /opt/perl-5.8.3/lib/site_perl/5.8.3/WWW/Mechanize.pm line 1052. Am I supposed to supply some parameters to redirect_ok?

Thanks for any help.

js1.

2006-01-06 Retitled by planetscape, as per Monastery guidelines
Original title: 'redirect_ok'

Replies are listed 'Best First'.
Re: Problem calling WWW::Mechanize redirect_ok
by Joost (Canon) on Jan 04, 2006 at 22:20 UTC

      Hi,

      Could you tell me how to override the redirect_ok method please? I'm not too sure how to do this.

      Thanks,

      js1

        Looking at the docs for LWP::UserAgent, we can see:
        $ua->redirect_ok( $prospective_request, $response )
        This method is called by request() before it tries to follow a redirection to the request in $response. This should return a TRUE value if this redirection is permissible. The $prospective_request will be the request to be sent if this method returns TRUE.

        So basically, you want to create a redirect_ok method that always returns a true value. A clean way of doing that is to create a subclass of WWW::Mechanize that overrides WWW::Mechanize's redirect_ok method.

        package WWW::Mechanize::AlwaysRedirect; # this class is a subclass of WWW::Mechanize use base 'WWW::Mechanize'; sub redirect_ok { 1; # always return true. } package main; my $agent = WWW::Mechanize::AlwaysRedirect->new(); # proceed as per usual with this $agent instead of the one # in your original code

        You seem to be slightly confused by object-oriented techniques. You should probably take a look at at perltoot and perlmod. There is also the very good "Object Oriented Perl" book by Damian Conway.

Re: Problem calling WWW::Mechanize redirect_ok
by mikeock (Hermit) on Jan 04, 2006 at 22:21 UTC
    In the Www::Mechanize documents, you see that redirect_ok() is an over loaded method of LWP::UserAgent. If you look LWP::UserAgent docs you will find the following

    $ua->redirect_ok( $prospective_request, $response ) This method is called by request() before it tries to follow a red +irection to the request in $response. This should return a TRUE value + if this redirection is permissible. The $prospective_request will be + the request to be sent if this method returns TRUE. The base implementation will return FALSE unless the method is in +the object's requests_redirectable list, FALSE if the proposed redire +ction is to a "file://..." URL, and TRUE otherwise.

    Hope this helps!

      Could you give me anymore clues please?! Sorry I'm probably being dim, but I'm struggling to pick out the answer!

      If I comment out the redirect_ok line, I get a page in the browser saying

      Found The document has moved here.

      where the text 'here' is a link to the site I want to redirect to.

      Thanks for anymore help!

        I wish I could give you further information, but I just browsed through the docs once I saw your question... Sorry

        Check out the docs for LWP::UserAgent and see what the variables are that are being called

Re: Problem calling WWW::Mechanize redirect_ok
by traveler (Parson) on Jan 04, 2006 at 23:45 UTC
Re: Problem calling WWW::Mechanize redirect_ok
by kulls (Hermit) on Jan 05, 2006 at 04:51 UTC
    seems  $agent is not return anything from the get method which cause to the error  Can't call method . Can you Dumper the  $agent before calling '$agent->redirect_ok()' method.
    -kulls

      I found the fix. The problem was in the headers. Here's my code:

      $results=$agent->get('http://lhrbod-apdhz001.ad.com/ess/'); print $results->{_headers}->as_string(); print "\n\n"; print $agent->content();