Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the landing page URL after redirections using axios

Tags:

node.js

axios

Using NodeJS, If I use axios with maxRedirects = 5, If I type a URL that will be redirected to another URL, how can I get the URL from the final landing page? In HTTP headers, when there is an HTTP 200 code, there is no header field for the landing page.

Example: if I use:

axios.get('http://stackoverflow.com/',config)
.then(function(response) { console.log(response);}

axios will automatically redirect to https://stackoverflow.com. So, how can get the final URL value "https://stackoverflow.com"?

Should I investigate in the returned object "response" and recreate the full url from domain and URI?

like image 896
Nicolas Guérinet Avatar asked Nov 22 '17 21:11

Nicolas Guérinet


2 Answers

I solved it by getting it from:

response.request.res.responseUrl

The Nicholas answer failed to get the port (like localhost:8080)

like image 73
MrFabio Avatar answered Sep 21 '22 13:09

MrFabio


Here is my quick and dirty solution on NodeJS. The starting URL is http://www.stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios , the final landing URL is https://stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios Please find the trick below to get the landing URL.

axios.get('http://www.stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios').then(function(response) {
      console.log(response.request.res.responseUrl);
     }).catch(function(no200){
      console.error("404, 400, and other events");
     
    });

response.request.res.responseURL is the right field in the JSON object that is returned by axios. If you run axios from a browser use console.log(response.request.responseURL);

like image 28
Nicolas Guérinet Avatar answered Sep 17 '22 13:09

Nicolas Guérinet