A.G.Progm.Enthusiast
A.G.Progm.Enthusiast

Reputation: 1010

How to get rid of HTTP 302 status code while reading a page in Perl?

I am trying to fetch the content of a webpage which requires authentication using LWP::UserAgent. However I am getting status as 302 Found and "The document has moved here

I have added push @{ $uagent->requests_redirectable }, 'GET'; statement but no help.

Below is my code:

use strict;
use warnings;
use LWP::UserAgent;

my $userid = "iamuser";
my $password = "thisispassword";
my $url = "http://some_website-requires-authentication";

my $uagent = new LWP::UserAgent();
push @{ $uagent->requests_redirectable }, 'GET';

my $req = HTTP::Request->new(GET=>"$url");
$req->authorization_basic($userid,$password);

my $response = $uagent->request($req);
print $response->status_line, "\n";
print $response->headers->as_string;

Output:

302 Found
Cache-Control: no-store
Connection: Keep-Alive
Date: Wed, 11 Aug 2018 04:43:10 GMT
Location: HTTP://my_url.website.com/page/bag
Server: website
Content-Length: 239
Content-Type: text/html; charset=iso-8859-1
Client-Date: Wed, 11 0 04:43:11 GMT
Client-Peer: 
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /CGB/ST=Larer Urban/L=Reds/O=COKPO CB Insurabce/CN=DO A  Secure SDL
Client-SSL-Cert-Subject: /C=UKBopp Drive/O=te Incorporated/OU=COPPER/CN=hula.website.com
Client-SSL-Cipher: AES128-SHA
Client-SSL-Socket-Class: IO::Socket::SSL
Client-Warning: Redirect loop detected (max_redirect = 7)
Keep-Alive: timeout=5, max=91
Set-Cookie: blablabla
Strict-Transport-Security: max-age=31536000
Title: 302 Found
X-Frame-Options: DENY

Need help on what I am doing wrong? Thanks in advance.

Upvotes: 0

Views: 989

Answers (1)

ikegami
ikegami

Reputation: 385799

One of the headers you received was

Client-Warning: Redirect loop detected (max_redirect = 7)

LWP detected that it was in an endless chain of redirects, and aborted. The web site probably relies on the cookies it provides being returned, but you didn't create a cookie jar for the agent to allow it to do that.

Replace

my $uagent = LWP::UserAgent->new();

with

my $uagent = LWP::UserAgent->new( cookie_jar => {} );

Upvotes: 1

Related Questions