node.js 如何使用 axios 进行 https 调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43240483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use axios to to make an https call?
提问by etayluz
I am trying to use axioswith a proxy server to make an https call:
我正在尝试使用带有代理服务器的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)})
The proxy servers I am using are all in the United States, highly anonymous, with support for HTTP and HTTPS.
我用的代理服务器都在美国,高度匿名,支持HTTP和HTTPS。
I am receiving this error:
我收到此错误:
{ Error: write EPROTO 140736580649920:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794:
{ 错误:写 EPROTO 140736580649920:错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议:../deps/openssl/openssl/ssl/s23_clnt.c:794:
In order to ensure that the problem is with axios and NOT the proxy, I tried this:
为了确保问题出在 axios 而不是代理上,我尝试了以下操作:
curl -x 52.8.172.72:4444 -L 'https://www.walmart.com/ip/50676589'
curl -x 52.8.172.72:4444 -L ' https://www.walmart.com/ip/50676589'
This totally works just fine.
这完全可以正常工作。
How do I configure axios to work with proxies and https URL's?
如何配置 axios 以使用代理和 https URL?
回答by cyberwombat
Axios https proxy support is borked if using https proxies. Try passing the proxy through [httpsProxyAgent][1]using http.
如果使用 https 代理,Axios https 代理支持将被取消。尝试[httpsProxyAgent][1]使用 http传递代理。
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))
Alternatively there is a fork of Axios that incorporates this: axios-https-proxy-fixbut I'd recommend the first method to ensure latest Axios changes.
或者,有一个 Axios 的分支包含了这个:axios-https-proxy-fix但我推荐第一种方法来确保最新的 Axios 更改。
回答by Denis Grushak
Try this. That work for me.
尝试这个。那对我有用。
First
第一的
npm install axios-https-proxy-fix
Then
然后
import axios from 'axios-https-proxy-fix';
const proxy = {
host: 'some_ip',
port: some_port_number,
auth: {
username: 'some_login',
password: 'some_pass'
}
};
async someMethod() {
const result = await axios.get('some_https_link', {proxy});
}
回答by Jan Molak
Try to explicitly specify the port in the URL:
尝试在 URL 中明确指定端口:
const url = "https://walmart.com:443/ip/50676589"
If you also need an HTTPS-over-HTTP tunnel, you'll find a solution in this article.
如果您还需要 HTTPS-over-HTTP 隧道,您将在本文中找到解决方案。
Hope this helps,
希望这可以帮助,
Jan
简
回答by Sergio
This error is because axios is trying to proxy your request via https (it takes it from your url), there is this ticket tracking it: https://github.com/axios/axios/issues/925
这个错误是因为 axios 试图通过 https 代理你的请求(它从你的 url 中获取),有这张票跟踪它:https: //github.com/axios/axios/issues/925
回答by Adam S
I lost a day of work when I updated my dependencies last week (Feb. 2020) trying to figure out why services were stalling. axios-https-proxy-fixwill cause Axios to hang indefinitely without timeout or throwing an error conflicting with other libraries in npm. Using node-tunnel (https://github.com/koichik/node-tunnel) to create an agent also works.
当我上周(2020 年 2 月)更新依赖项试图找出服务停止的原因时,我失去了一天的工作。axios-https-proxy-fix将导致 Axios 无限期挂起而不会超时或引发与 npm 中其他库冲突的错误。使用 node-tunnel ( https://github.com/koichik/node-tunnel) 创建代理也有效。
const tunnel = require('tunnel');
class ApiService {
get proxyRequest() {
const agent = tunnel.httpsOverHttp({
proxy: {
host: 'http://proxy.example.com',
port: 22225,
proxyAuth: `username:password`,
},
});
return axios.create({
agent,
})
}
}

