Axios - 从响应对象中获取请求 URI 数据 [英] Axios - get request URI data from the response object

查看:35
本文介绍了Axios - 从响应对象中获取请求 URI 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试将节点项目从使用 request.js 迁移到使用 axios.js

So, I'm trying to migrate a node project from using request.js to use axios.js

当我使用请求包时,我可以像这样从响应对象中获取 URI 数据

When I'm using the request package I'm able to get the URI data from the response object like this

request(req, (err, response) => {
  if (err) console.error(err);
  console.log(response.statusCode);
  console.log(response.request.uri);
  console.log(response.request.uri.hash);
  console.log(response.request.uri.host);
});

但是当我像这样使用 axios 时

But when I use axios like this

axios(req)
  .then(response => {
    console.log(response.status);
    console.log(response.request.uri);
  })
  .catch(err => console.error(err));

对于 response.request.uri

那么,我是否使用了 axios 包错误,还有另一种方法可以获取我想要的信息,还是 axios 不支持??

So, Am I using the axios package wrong and there is another way to get that information I want or axios just doesn't support that ??

推荐答案

在您从 res.config.url 获得 url 后添加到@Matt Weber 的答案中,您可以使用 url.parse 直接解析它并从那里访问 .hash .query.

Adding to @Matt Weber's answer after you get the url from res.config.url you can directly parse it using url.parse and access the .hash .query from there.

例如:

const url = require('url')
console.dir(url.parse('protocol://host.com/?q=q1#hash'));

// Outputs:
Url {
  protocol: 'protocol:',
  slashes: true,
  auth: null,
  host: 'host.com',
  port: null,
  hostname: 'host.com',
  hash: '#hash',
  search: '?q=q1',
  query: 'q=q1',
  pathname: '/',
  path: '/?q=q1',
  href: 'protocol://host.com/?q=q1#hash'
}

这篇关于Axios - 从响应对象中获取请求 URI 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆