How to use axios to make an https call?

74,573

Solution 1

Axios https proxy support is borked if using https proxies. Try passing the proxy through [httpsProxyAgent][1] using 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-fix but I'd recommend the first method to ensure latest Axios changes.

Solution 2

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

Solution 3

You can solve this problem looking this issue

At this solution instead use the proxy interface, use the http(s)Agent. For it the solution use the native node module 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
};

For it works the proxy property must be equal to false.

Solution 4

The https-proxy-agent and node-tunnel solutions did work for me, but both of them doesn't support conditional proxying using NO_PROXY.

I found global-agent as the best solution in my case as it modifies the core http and https objects and will be applied automatically to any library that makes use of them, including axios, got, request, etc.

The usage is very simple.

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

Add import 'global-agent/bootstrap'; to the entrypoint (index.ts) of the server.

Run with these env vars and make sure HTTP_PROXY, HTTPS_PROXY are NOT in the env.

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

This is how I finally ended up using it.

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

Solution 5

Try to explicitly specify the port in the 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.

Hope this helps,

Jan

Share:
74,573
etayluz
Author by

etayluz

Updated on May 11, 2021

Comments

  • etayluz
    etayluz almost 3 years

    I am trying to use axios with a proxy server to make an https call:

    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.

    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:

    In order to ensure that the problem is with axios and NOT the proxy, I tried this:

    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?