Reputation: 2226
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?
Upvotes: 15
Views: 24787
Reputation: 1624
I solved it by getting it from:
response.request.responseURL
The MrFabio's answer failed for me.
Upvotes: 6
Reputation: 2226
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);
Upvotes: 21