node.js - 如何在使用 axios 重定向后获取着陆页 URL

标签 node.js axios

使用 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/

相关文章:

node.js - Node.js 中的 Dall-E API 总是返回 400 Bad Request

javascript - 在node js中查找最近创建的目录

json - 如何使用流对 Node.js 中的大型嵌套对象进行 JSON 字符串化?

Node.js 串行端口 - 类型错误 : "offset" is not an integer

javascript - Node.js 的回调函数都是异步的吗?

javascript - 如何在浏览器上下载文件

javascript - Vue 对象未使用 axios POST 请求更新

node.js - Promise.all() 和并行 promise 在 Node 上不同

javascript - 在 vue.js 中使用 v-if 时,为什么我的 API 数据显示为未定义?

node.js - 连接到 API 时 Axios CERT_HAS_EXPIRED 错误,但仅来自 1 个服务器