Perl: LWP :: UserAgent always returns 200 for redirected URLs

I have a simple url that has 302 temp. go to another page.

I am trying to get if the URL returns the code 200 (for OK) to restore it, and stop if something other than 200 is returned.

My code is:

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 it is 302. I tested the header response with FireBug in Firefox. The URL returns β€œ302 Moved Temporarily” in the Net module in FireBug. But the code above in perl returns 200. Why?

+6
source share
1 answer

LWP :: UserAgent automatically follows an HTTP redirect . You can disable this behavior by passing the 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; 
+17
source

Source: https://habr.com/ru/post/903381/


All Articles