Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: LWP::UserAgent returns always code 200 for redirected urls

I have a simple url which does a 302 temp. move to another page.

I try to get to if the URL returns code 200 (for OK) to retrieve it and to stop if something else than 200 is returned.

My code:

my $ua = LWP::UserAgent->new( env_proxy => 1,keep_alive => 1, timeout => 30, agent => "Mozilla/4.76 [en] (Win98; U)");
my $response = $ua->get( $currenturl);
print $response->code;

The code above ALWAYS returns 200, even if its 302. I tested the header response using FireBug in Firefox. The URL returns "302 Moved Temporarily" in the Net module in FireBug. But the code above in perl returns 200. Why?

like image 379
NovumCoder Avatar asked Dec 08 '11 14:12

NovumCoder


1 Answers

LWP::UserAgent automatically follows HTTP redirects. You can disable such behavior by passing max_redirect option set to 0.

my $ua = LWP::UserAgent->new( max_redirect => 0, env_proxy => 1,keep_alive => 1, timeout => 30, agent => "Mozilla/4.76 [en] (Win98; U)");
my $response = $ua->get( $currenturl);
print $response->code;
like image 91
Alexander Zhuravlev Avatar answered Oct 13 '22 00:10

Alexander Zhuravlev