【问题标题】:How to use axios to make an https call?如何使用 axios 进行 https 调用?
【发布时间】:2017-08-31 14:47:51
【问题描述】:

我正在尝试将 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:error: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?

【问题讨论】:

    标签: node.js ssl curl proxy axios


    【解决方案1】:

    this issue就可以解决这个问题

    在此解决方案中,请改用代理接口,使用 http(s)Agent。 为此,解决方案使用本机节点模块https-proxy-agent

    var ProxyAgent = require('https-proxy-agent');
    var axios = require('axios');
    
    const agent = ProxyAgent('http://username:pass@myproxy:port')
    
    var config = {
      url: 'https://google.com',
      proxy: false,
      httpsAgent: agent
    };
    

    要正常工作,代理属性必须等于 false。

    【讨论】:

      【解决方案2】:

      我知道这是一篇旧帖子,但我希望这个解决方案可以为任何面临Axios 的 SSL 问题的人节省时间。

      • 你可以使用HTTP代理,我建议使用hpagent
      const axios = require("axios");
      const { HttpProxyAgent, HttpsProxyAgent } = require("hpagent");
      
      async function testProxy() {
        try {
          const proxy = "http://username:password@myproxy:port";
          // hpagent configuration
          let agentConfig = {
            proxy: proxy,
            // keepAlive: true,
            // keepAliveMsecs: 2000,
            // maxSockets: 256,
            // maxFreeSockets: 256,
          };
          axios.defaults.httpAgent = new HttpProxyAgent(agentConfig);
          axios.defaults.httpsAgent = new HttpsProxyAgent(agentConfig);
          // Make a simple request to check for the IP address.
          let res = await axios.get("https://api.ipify.org/?format=json");
          console.log(res.data);
        } catch (error) {
          console.error(error);
        }
      }
      
      testProxy();
      

      【讨论】:

        【解决方案3】:

        https-proxy-agentnode-tunnel 解决方案确实对我有用,但它们都不支持使用 NO_PROXY 的条件代理。

        我发现 global-agent 是我的最佳解决方案,因为它修改了核心 http 和 https 对象,并将自动应用于任何使用它们的库,包括 axiosgotrequest等。

        用法很简单。

        npm i global-agent
        npm i -D @types/global-agent
        

        import 'global-agent/bootstrap'; 添加到服务器的入口点 (index.ts)。

        使用这些环境变量运行并确保 HTTP_PROXY、HTTPS_PROXY 不在环境中。

        export GLOBAL_AGENT_NO_PROXY='*.foo.com,baz.com'
        export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080
        

        这就是我最终使用它的方式。

        import { bootstrap } from 'global-agent';
        
        const proxy = process.env.EXTERNAL_PROXY;
        
        if (proxy) {
          process.env.GLOBAL_AGENT_HTTP_PROXY = proxy;
          process.env.GLOBAL_AGENT_NO_PROXY = process.env.NO_PROXY;
          process.env.GLOBAL_AGENT_FORCE_GLOBAL_AGENT = 'false';
          bootstrap();
          logger.info(`External proxy ${proxy} set`);
        }
        

        【讨论】:

          【解决方案4】:

          上周(2020 年 2 月)更新我的依赖项试图找出服务停滞的原因时,我浪费了一天的工作。 axios-https-proxy-fix 将导致 Axios 无限期挂起而不会超时或抛出与 npm 中其他库冲突的错误。使用节点隧道 (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,
              })
            }
          }
          

          【讨论】:

            【解决方案5】:

            尝试在 URL 中明确指定端口:

            const url = "https://walmart.com:443/ip/50676589"
            

            如果您还需要 HTTPS-over-HTTP 隧道,您可以在 this article 中找到解决方案。

            希望这会有所帮助,

            一月

            【讨论】:

            • 为什么要投反对票? Axios 有(有?)一个问题,指定端口号会强制它使用 https 代理而不是 http 代理。
            【解决方案6】:

            试试这个。这对我有用。

            第一

            npm install axios-https-proxy-fix
            

            然后

            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});
            }
            

            【讨论】:

              【解决方案7】:

              如果使用 https 代理,Axios https 代理支持会被破坏。尝试使用 http 通过[httpsProxyAgent][1] 传递代理。

              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 更改。

              【讨论】:

              • url 应该在配置之外,否则你会得到错误“The url argument must be of type string”。
              • @cyberspace 你在说什么?请求方法需要一个配置对象。你用 get/post 试过了吗?因为那些确实需要单独的网址。文档:github.com/axios/axios#request-method-aliases
              • 在我的情况下还必须在配置中添加 proxy: false
              【解决方案8】:

              这个错误是因为 axios 试图通过 https 代理你的请求(它从你的 url 中获取),有这张票在跟踪它:https://github.com/axios/axios/issues/925

              【讨论】:

                猜你喜欢
                • 2019-09-22
                • 2016-03-05
                • 2014-03-17
                • 2021-07-24
                • 1970-01-01
                • 2021-07-11
                • 1970-01-01
                • 2014-04-10
                • 1970-01-01
                相关资源
                最近更新 更多