使用 NodeJS,如果我将 axios 与 maxRedirects = 5 一起使用,如果我键入一个将被重定向到另一个 URL 的 URL,我如何从最终登陆页面获取 URL? 在 HTTP header 中,当有 HTTP 200 代码时,着陆页没有 header 字段。
示例:如果我使用:
axios.get('http://stackoverflow.com/',config)
.then(function(response) { console.log(response);}
axios 会自动重定向到https://stackoverflow.com .那么,如何才能得到最终的 URL 值“https://stackoverflow.com”呢?
我应该调查返回的对象“response”并根据域和 URI 重新创建完整的 url 吗?
最佳答案
这是我在 NodeJS 上的快速而肮脏的解决方案。起始网址为 http://www.stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios
,最后登陆网址是
https://stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios
请找到下面的技巧来获取登陆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是axios返回的JSON对象中的右字段。如果您从浏览器运行 axios,请使用 console.log(response.request.responseURL);
关于node.js - 如何在使用 axios 重定向后获取着陆页 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47444251/