Search code examples
perlwww-mechanize

Perl WWW::Mechanize exits on 503 Service Unavailable


I am using Perl's WWW::Mechanize to send HTTP traffic to a site. It returns 503 Service Unavailable error as a result of the HTTP request sent to the site.

The problem is that the script dies after receiving this error. I do not want that to happen and instead the script should continue the execution. I want it to ignore that error.

$mech=WWW::Mechanize->new();
$mech->agent_alias('Windows IE 6');

$mech->get($url);

// code after this does not execute

print $mech->content();

How can I configure WWW::Mechanize to ignore the HTTP response code 503 and continue the execution?

Thanks.


Solution

  • my $mech = WWW::Mechanize->new( onerror => undef );
    
    $mech->get($url);
    if (!$mech->success()) {
        die("$url: ".$mech->res->status_line());
    }
    
    print $mech->content();
    

    Replace the die with whatever action you want to have take instead.