如何使用 axios 进行 https 调用?

新手上路,请多包涵

我正在尝试将 axios 与代理服务器一起使用来进行 https 调用:

 const url = "https://walmart.com/ip/50676589"
var config = { proxy: { host: proxy.ip, port: proxy.port } }

axios.get(url, config)
.then(result => {})
.catch(error => {console.log(error)})

我使用的代理服务器都在美国,高度匿名,支持 HTTP 和 HTTPS。

我收到此错误:

{ 错误:写入 EPROTO 140736580649920:错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议:../deps/openssl/openssl/ssl/s23_clnt.c:794:

为了确保问题出在 axios 而不是代理,我尝试了这个:

curl -x 52.8.172.72:4444 -L ‘ https://www.walmart.com/ip/50676589

这完全可以正常工作。

如何配置 axios 以使用代理和 https URL?

原文由 etayluz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3.8k
1 个回答

如果使用 https 代理,Axios https 代理支持就会失效。尝试使用 http 通过 httpsProxyAgent 传递代理。

 var axios = require('axios');

let httpsProxyAgent = require('https-proxy-agent');
var agent = new httpsProxyAgent('http://username:pass@myproxy:port');

var config = {
  url: 'https://google.com',
  httpsAgent: agent
}

axios.request(config).then((res) => console.log(res)).catch(err => console.log(err))

或者,有一个 Axios 的分支合并了这个: axios-https-proxy-fix 但我推荐第一种方法以确保最新的 Axios 更改。

原文由 cyberwombat 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题