CURL --在node.js axios中解析

问题描述 投票:0回答:2

纯 CURL 中的请求示例:

curl https://www.example.com --resolve www.example.com:443:127.0.0.1

Node.js 代码(缺少“解析”功能):

const axios = require('axios');
axios({
  method: 'GET',
  url: 'https://www.example.com'
}).then((response) => {
  console.log(response);
}).catch((error) => {
  console.error(error);
});

如何将

resolve
选项传递给 Node.js Axios?我在他们的自述文件中找不到任何相关信息。

有这样的功能吗?如果没有,NPM 包有什么作用?

node.js curl axios resolve
2个回答
1
投票

Axios 没有内置此功能。

但是你可以尝试在标头中传递主机?

我需要 https 作为示例来阻止 ssl 问题。

const axios = require('axios')
const https = require('https')

const handler = async () => {
    const response = await axios.get('https://www.example.com', {
        headers: { Host: '127.0.0.1:443' },
        httpsAgent: new https.Agent({
            rejectUnauthorized: false,
        }),
    })

    console.log(response)
}

handler()


0
投票

Axios 使用 Node 内置的

http
/
https
模块。要使用的
http.Agent
https.Agent
的实例可以在传递给 axios.create()
config 对象
中传递。如果不存在,这些对象将被初始化为默认值。您可以指定使用 a config
 构造的 
https.Agent 对象,其中
servername
字段填充您想要使用的值而不是主机名。

© www.soinside.com 2019 - 2024. All rights reserved.