Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a method in lwp::useragent that tells you where you were redirected to after using get?

janitored by ybiC: Fix typo (lwd::useragent) and linkify

Replies are listed 'Best First'.
Re: detect redirect
by ysth (Canon) on Dec 12, 2004 at 10:27 UTC
    I think that's available from the response object returned. The lwp-request script has a -S option that shows the info; take a look and see what it does.

    Update: it does:

    $response = $ua->request($request); ... my $method = $response->request->method; my $url = $response->request->url->as_string; my $code = $response->code; print "$method $url --> ", $response->status_line, "\n";
    (only it does the last 4 lines depth-first recursively, using $response->previous to look up earlier redirect $response's). Hmm, $code doesn't seem to be used anywhere there...
Re: detect redirect
by sgifford (Prior) on Dec 12, 2004 at 18:16 UTC

    If you've asked LWP to automatically follow redirects, it looks like the HTTP::Response request method will tell you the actual request that resulted in the page you ended up with, and you can use the previous method to walk backwards through all of the redirects that were done.

    If it's not following redirects for you, you can use the is_redirect method to determine if the response was a redirect, then look at $r->header('Location') to see where the redirect was to.