Axios: Request to HTTPS with HTTP proxy fails

Created on 30 May 2017  Â·  52Comments  Â·  Source: axios/axios

Summary

Trying to do a HTTPS request with a HTTP proxy fails.

const req = await axios({
  url: 'https://somedomain.com',
  proxy: {
    host: '89.151.146.7',
    port: 6060,
    auth: {
      username: 'myname',
      password: 'mypass',
    },
  },
});

Results in:

Error: write EPROTO 140736379442112:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794

The problem is already described by @chovy in this ticket, which ended up closed as OP did not have the same problem: https://github.com/mzabriskie/axios/issues/662

@chovy says:

I still have this issue not being able to hit an https url with an http proxy. I can do it fine in request and also in curl from shell. Something not working with axios. I get an EPROTO error.

 Context

  • axios version: v0.16.1
  • Environment: node v7.10.0, Mac OSX Sierra

Most helpful comment

Had a similar issue recently, my solution was to use an HTTPS-over-HTTP tunnel, specify port 443 explicitly in the URL and disable automatic proxy detection in axios:

import axios, { AxiosInstance } from 'axios';
import * as tunnel from 'tunnel';

const tunnel = tunnel.httpsOverHttp({
    proxy: {
        host: 'proxy.mycorp.com',
        port: 8000,
    },
});

const httpClient: AxiosInstance = axios.create({
    baseURL: 'https://some.api.com:443',
    httpsAgent: tunnel,
    proxy: false,
});

More about the solution in this article.

Hope this helps,
Jan

All 52 comments

I'm having the same problem of https request over proxy. I do not want to move to request.js.

return axios({ url: 'https://site.com', proxy: { host: 'proxy.xxxxxx.com', port: 3128 } })
Result:
Error: write EPROTO 139800246822688:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794

request.js and curl in this situation works well.

Context

  • axios version: v0.16.1
  • Environment: node v6.9.5, CentOS release 6.8 (Final)

Me and a colleague of mine identified the problem, a PR should be coming soon

Same issue here: Requesting https resources behind an http proxy.

axios.get('https://resources.json', {   
   proxy: {
      host: 'http://my.proxy.com',
      port: 12345
   }
})
   .then(() => {})
   .catch(() => {});

Result:
Error: getaddrinfo ENOTFOUND http://my.proxy.com http://my.proxy.com:12345

Any progress on this so far?

Same problem here.

Any news ?

My problem was not related to proxies tho, I had to send GET request to https domain which wasn't certified. In curl inside linux terminal, solution was to use -k or --insecure flag. I couldn't find Axios solution tho, but with request lib I done it like so; might give you idea:

const agentOptions = {
    host: '10.100.0.10',
    port: '443',
    path: '/',
    rejectUnauthorized: false,
  }

const agent = new https.Agent(agentOptions)
request({
      url: 'https://10.100.0.10/myUncertifiedTargetUrl',
      method: 'GET',
      agent,
    }, (err, resp, body) => {
...

Feel free to use this until they get #959 merged.

Same here, btw @mistermoe's PR works fine

@mistermoe I just left work without making it succeeded :(

I use this package : https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger

And if I replace "axios" by yours, the following error disappears:
"Error: write EPROTO 139800246822688:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794"

But I have something like:
"Error: getaddrinfo EAI_AGAIN"

I notice that the Messenger plugin makes requests like that:
`
let client = axios.create({ ... });

client.get('...')
`

Did you succeed by this way or not directly by Axios "request" function?

Thanks!

EDIT: Another one having this issue:
https://github.com/axios/axios/pull/959#issuecomment-337595602

Any chance of merging this? Could we leverage the newly added config.transport to avoid this issue?

ups, same problem here!

Unfortunately, this pull request doesn't merged, but I fixed this issue https://github.com/Sitronik/axios

Had a similar issue recently, my solution was to use an HTTPS-over-HTTP tunnel, specify port 443 explicitly in the URL and disable automatic proxy detection in axios:

import axios, { AxiosInstance } from 'axios';
import * as tunnel from 'tunnel';

const tunnel = tunnel.httpsOverHttp({
    proxy: {
        host: 'proxy.mycorp.com',
        port: 8000,
    },
});

const httpClient: AxiosInstance = axios.create({
    baseURL: 'https://some.api.com:443',
    httpsAgent: tunnel,
    proxy: false,
});

More about the solution in this article.

Hope this helps,
Jan

@jan-molak My fix is easy to use:

  1. npm install axios-https-proxy-fix
  2. 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});
}

@jan-molak's solution worked for me as well. It would really be nice to not have to force the port number on the url though.

@jan-molak @Sitronik I tried your solution but get Error: socket hang up.
What I want is see how the request go through charles,so I open charles,and set proxy to {host:'127.0.0.1',port:8888} in my code. I don't know whether it is suitable to use your solution

Any chance to be included in 0.19.0?

@astappev this definitely looks like it needs a fix ASAP. There's a lot of related issues and PRs that I need to sift through, but I'll add it to my 0.19.0 roadmap.

@emilyemorehouse, do you have any estimation when it will be released?
The PR #959 solve critical part of the issue, other issues like proxying redirections and so can be improved later. The changes in #959 are pretty simple and can be published in just few minutes.

@Sitronik that works for me thanks

@jan-molak 's fix saved my day.
+1 to merging the PR.

Another +1 to merging this!!

Same issue... please merge!

Same issue here

@Sitronik that works for me thanks - please merge!
However the proxy settings does not seem to be read from environment variables (http_proxy / https_proxy) but have to be specified as part of AxiosRequestConfig.

+1 Still using work arounds mentioned above but would love to see this work out of the box.

+1 When you fix this? But we need no_proxy too...

@Sitronik please add PR. Your package axios-https-proxy-fix works just fine. axios.defaults.proxy = {host: ip, port: port}

Still an issue, would use that package but its many commits behind? Would that be a non issue :S

would use that package but its many commits behind?

Same for me while waiting for that change I'm going for node-fetch + httpsProxyAgent + bluebird. Works fine.

@Makoehle

Hey I ended up getting it to work, try export http and https to "" on your terminal

export http_proxy="" && export https_proxy=""

Thank you I know about env variables. I tried everything. My guess is it has to do with #1207

Waiting for merge too. Why it is so long?

959 was merged and is available in 0.19.0-beta.1

I can not solve it out in 0.19.0-beta.1, but I fixed like this

// default axios usage
import * as ProxyAgent from "proxy-agent";
const proxyAgent = new ProxyAgent(process.env.HTTPS_PROXY);  // http://127.0.0.1:1080
Axios.defaults.httpsAgent = proxyAgent;
Axios.defaults.proxy = false;

// google api usage
const Sheets = google.sheets({
    version: "v4",
    auth: key,
    httpsAgent: /dev/.test(process.env.NODE_ENV) ? proxyAgent : false,
    proxy: false
});

0.19.0-beta.1 should fix the issue, I am closing the issue and feel free to create a new one if anyone still having the issue

@Khaledgarbaya I am still having this issue with 0.19.0-beta.1
My question is what should my proxy request look like (and how does the isHttps flag work?).

I assumed that for HTTPS over HTTP I would simple set the http url / port for the proxy like this:

axiosRequestOptions.proxy = {
            host: '192.0.01',
            port: '8888',
            // isHttps: true, ?
        }

That being said the above still fails so I am wondering if it is a configuration / documentation issue where my structuring of the proxy property / object is the cause.

@necevil
host: '192.0.01'

The 0.19.0-beta.1 still does not work. I kept getting Request failed with status code 400. Wasted a lot of time on this. Switched to the request library and it worked like a charm on the first try.

Ran into the same problem.

const axios = require('axios-https-proxy-fix')

fixed the problem for me.

After updating to 0.18.0 this seems to be working for me. I had to specify the following options for my use case though:

axios.get('<some_url_on_corporate_intranet>', {
        proxy: false,
        httpsAgent: https.Agent({
            rejectUnauthorized: false // Allows the use of self-signed certificates (not recommended)
    })

tried all solution and all failed base on axios 0.18
final solution :
install nginx module ngx_http_proxy_connect_module
then request https url via http proxy with request-promise

You don’t need Nginx if you’re using request. Request handled proxies fine.

-
Anthony

On Jan 17, 2019, at 8:44 PM, redbearder notifications@github.com wrote:

tried all solution and all failed base on axios 0.18
final solution :
install nginx module ngx_http_proxy_connect_module
then request https url via http proxy with request-promise

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

You don’t need Nginx if you’re using request. Request handled proxies fine. - Anthony
…
On Jan 17, 2019, at 8:44 PM, redbearder @.*> wrote: tried all solution and all failed base on axios 0.18 final solution : install nginx module ngx_http_proxy_connect_module then request https url via http proxy with request-promise — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Thank you for reply
I assume that people will send requests via single IP address as proxy gateway, that is what I will do
However request-promise catch a error "RequestError: Error: tunneling socket could not be established, code 400"
so I dont find solution from Node and then I try to fix it from Gateway side
and the solution comes out above

2 years later, still broken!!!

Use request promise.

Anthony
http://profullstack.com

On Jul 18, 2019, at 7:05 AM, Yassien notifications@github.com wrote:

2 years later, still broken!!!

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

Here's what i did for those who stumble upon this and still want to use axios:

const HttpsProxyAgent = require("https-proxy-agent"),
      axios = require("axios");

const httpsAgent = new HttpsProxyAgent({host: "proxyhost", port: "proxyport", auth: "username:password"})

//use axios as you normally would, but specify httpsAgent in the config
axios = axios.create({httpsAgent});

This will let you make https requests over an http proxy, simple and neat.

Still having the same issue after 2 years.
Is someone working on this or can we join to contribute?

Just ran into this issue myself. Hoping you guys can fix this at some point.
czl032405's solution of using proxy-agent worked for me.

According to the 0.19.0 release, this has been fixed. Since it obviously hasn't, a new issue should be opened

This functionality is still broken in axios 0.19.
Version 0.19.0 fails with the message _"Error: Protocol "http:" not supported. Expected "https:""_
Version 0.19.1 fails with _"Error: timeout of 1000ms exceeded"_. Increasing the timeout doesn't help. The proxy server expects a CONNECT request while axios sends a GET request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zackseuberling picture zackseuberling  Â·  83Comments

youurayy picture youurayy  Â·  69Comments

kyeotic picture kyeotic  Â·  64Comments

adl1995 picture adl1995  Â·  143Comments

mzabriskie picture mzabriskie  Â·  54Comments