Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get redirected url in perl

Tags:

url

get

perl

I want to get last of redirect URL.

like

url_1 : http://on.fb.me/4VGeu url_2 : https://www.facebook.com/

I want to get url_2 by url_1 in perl. Previous source is below.

sub get_redirect_location
{
    my ($url) = @_;
    my $ua = LWP::UserAgent->new;

    $ua->proxy('http', 'SAMPLE_PROXY');
    my $req = new HTTP::Request(GET => $url);
    my $res = $ua->request($req);

    return $res->headers_as_string;
}

Thanks in advance.

like image 912
hoshipeace Avatar asked Dec 02 '25 09:12

hoshipeace


1 Answers

You can find the request that lead to a response using

$response->request()

You can get the previous response in the chain using

$response->previous()

All together:

while ($response) {
   say $response->request()->uri();
   $response = $response->previous();
}
like image 56
ikegami Avatar answered Dec 03 '25 23:12

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!