Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How to get real address with LWP and redirects?

by ivanatora (Sexton)
on Sep 11, 2007 at 12:40 UTC ( #638285=perlquestion: print w/replies, xml ) Need Help??
ivanatora has asked for the wisdom of the Perl Monks concerning the following question:

I use LWP::UserAgent to get some site contents and go through some links. The question is sometimes links are, which redirects to another page (mod_rewrite for sure) and LWP gets its contents perfectly, but I want to know the new location address. Can I do it with LWP?
  • Comment on How to get real address with LWP and redirects?

Replies are listed 'Best First'.
Re: How to get real address with LWP and redirects?
by ikegami (Pope) on Sep 11, 2007 at 13:35 UTC

    To see the final uri:

    my $response = $ua->...; print($response->request()->uri(), "\n");

    Or to see all the redirections:

    my $response = $ua->...; while ($response) { print($response->request()->uri(), "\n"); $response = $response->previous(); }
Re: How to get real address with LWP and redirects?
by szbalint (Friar) on Sep 11, 2007 at 13:35 UTC

    Sure. It's a bit tricky to wrap your head around it, because the HTTP::Response object you received contains the HTTP::Request object that was created when LWP encountered the (last) redirect. The request object contains the url you've been redirected to. It might be confusing to see that the request object can change, but it does so just as an added note, people shouldn't rely on it for example to determine the original url.

    Anyway, you can access the url something like this:


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://638285]
Approved by Corion
[Tanktalus]: Discipulus: I'd have tried my hand at it, but OP said something about not replying if you're experienced at regexes :)
[Tanktalus]: And now that I've been using regexes in C++, I'm thinking that might apply :)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-12-18 23:15 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (501 votes). Check out past polls.