I can now say that the problem indeed is that LWP was following redirects (as it should). But also myself was also following redirects by issuing another request via LWP.

So how I solved it was to set

$ua->requests_redirectable([]);
which tells LWP::UserAgent not to follow any redirects for any request.

(setting

$ua->requests_redirectable(['GET']);
would allow only GET to be followed by LWP).

I have also discovered that there is another problem with allowing UA to follow redirects. In a redirect the server sends a Location header which contains the url of the redirect and issues a 302 status (or 30X something). UA extracts this Location url and issues another request to there.

The problem lies in the server sometimes sending a relative url back. And UA tries to make it absolute. In my case, UA failed to do that. So even if I allowed UA to follow redirect, it would have failed in sending a malformed url to the server.

UA has the following code to convert the url:

my $referral_uri = $response->header('Location'); { # Some servers erroneously return a relative URL for redir +ects, # so make it absolute if it not already is. local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1; my $base = $response->base; $referral_uri = "" unless defined $referral_uri; $referral_uri = $HTTP::URI_CLASS->new($referral_uri, $base)->abs($ba +se); } $referral->uri($referral_uri);

In my case:

base='http://server.com/ABC/afilename1?op=678' referral='../../ABC/XYZ/KLM/afilename2?aa=123'
and the calculated new referral came out as:
http://server.com/../ABC/XYZ/KLM/afilename2?aa=123

instead of the correct one of:

http://server.com/ABC/XYZ/KLM/afilename2?aa=123

may be this is expected behaviour from URI->abs()?< I will send a bug report just in case.

Thanks Monks


In reply to Re^3: GET request using LWP::UserAgent returns 200 OK but Firefox 302 Found by bliako
in thread GET request using LWP::UserAgent returns 200 OK but Firefox 302 Found by bliako

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.