etayluz
etayluz

Reputation: 16436

How to use axios to make an https call?

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?

Upvotes: 38

Views: 103827

Answers (8)

cyberwombat
cyberwombat

Reputation: 40153

Axios https proxy support is borked if using https proxies. Try passing the proxy through httpsProxyAgent using http.

const axios = require('axios'); 
const httpsProxyAgent = require('https-proxy-agent');

const httpsAgent = new httpsProxyAgent('http://username:pass@myproxy:port');
// or const httpsAgent = new httpsProxyAgent({ host: 'myproxy', port: 9999 });

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

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.

Upvotes: 43

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.

Upvotes: 9

Abderrahmane M
Abderrahmane M

Reputation: 41

I know this is an old post, but I hope this solution saves time for anyone facing an SSL issue with Axios.

  • You can use an HTTP agent, I suggest using 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();

Upvotes: 2

Sidharth V
Sidharth V

Reputation: 181

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

Upvotes: 2

Adam S
Adam S

Reputation: 501

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-fix will 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.

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

Upvotes: 0

Jan Molak
Jan Molak

Reputation: 4536

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

Upvotes: 1

Denis Grushak
Denis Grushak

Reputation: 400

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

Upvotes: 13

Sergio
Sergio

Reputation: 501

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

Upvotes: 0

Related Questions