使用 axios 重定向后如何获取着陆页 URL [英] how to get the landing page URL after redirections using axios
问题描述
使用 NodeJS,如果我使用 maxRedirects = 5 的 axios,如果我输入的 URL 将被重定向到另一个 URL,我如何从最终登录页面获取该 URL?在 HTTP 标头中,当有 HTTP 200 代码时,着陆页没有标头字段.
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.
示例:如果我使用:
axios.get('http://stackoverflow.com/',config)
.then(function(response) { console.log(response);}
axios 会自动重定向到 https://stackoverflow.com.那么,如何获得最终的 URL 值https://stackoverflow.com"?
axios will automatically redirect to https://stackoverflow.com. So, how can get the final URL value "https://stackoverflow.com"?
我是否应该在返回的对象响应"中进行调查并从域和 URI 重新创建完整的 url?
Should I investigate in the returned object "response" and recreate the full url from domain and URI?
推荐答案
这是我关于 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 的技巧.
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 是 axios 返回的 JSON 对象中的正确字段.如果您从浏览器运行 axios,请使用 console.log(response.request.responseURL);
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);
这篇关于使用 axios 重定向后如何获取着陆页 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!