Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios proxy is not working. #2072

Closed
pumanitro opened this issue Mar 31, 2019 · 21 comments
Closed

Axios proxy is not working. #2072

pumanitro opened this issue Mar 31, 2019 · 21 comments

Comments

@pumanitro
Copy link

Describe the bug
I have installed anyProxy 3rd party proxy ( http://anyproxy.io/en/#install )
When I type anyproxy in my command line then my proxy starts on http://localhost:8001
and I have also preview of proxy status on http://localhost:8002.

On the Frontend site, I'm using axios like:

axios
      .get("https://randomuser.me/api/?results=50", {
        proxy: {
          host: "http://localhost",
          port: 8001
        }
      })
      .then(response => {
        const data = response.data.results;
        this.setState({ data });
      })
      .catch(error => {
        console.log(error);
      });

Here is a simplified version of this:
https://codesandbox.io/s/j35zl05lk5

But the request is omitting proxy and is sent directly:
image

It behaves like the axios proxying functionality would not be even working.

I also tried stuff like:

let httpsProxyAgent = require("https-proxy-agent");
var agent = new httpsProxyAgent("http://localhost:8001");
...
 axios
      .get("https://randomuser.me/api/?results=50", {
        httpAgent: agent
      })
      .then(response => {
        const data = response.data.results;
        this.setState({ data });
      })
      .catch(error => {
        console.log(error);
      });

But it gives me the same result as the previous one.
Also tried npm dependency called "axios-https-proxy-fix", but with the same result.

Expected behavior
Proxying request to the proxy server.

Environment:

  • Axios Version "axios": "0.18.0",
  • OS: Win 10
  • Browser Chrime
  • Browser Version 73.0.3683.86
@pumanitro pumanitro added the bug label Mar 31, 2019
@SwiTool
Copy link

SwiTool commented May 7, 2019

I can confirm. Full post on https://stackoverflow.com/questions/55981040/axios-https-request-over-a-proxy

I tried the proxy param, which is apparently not supported in browser, but at least I tried :

const axios = require('axios');

var res = await axios.get('https://api.ipify.org?format=json', {
    proxy: {
        host: 'proxy-url',
        port: 80,
        auth: {username: 'my-user', password: 'my-password'}
    }
});
console.log(res.data); // gives my ip and not the proxy's one

And also with the httpsAgent param :

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

var agent = new HttpsProxyAgent('http://my-user:my-pass@proxy-url:port');
var res = await axios.get('https://api.ipify.org?format=json', {
    httpsAgent: agent,
});
console.log(res.data); // gives my ip and not the proxy's one

None of them work.

@k0t0fey
Copy link

k0t0fey commented May 17, 2019

+1
proxy option not working with https
Any ideas how to solve this problem?

@jerrwy
Copy link

jerrwy commented May 23, 2019

+1

@kingkong404
Copy link

+1 not working in version .19

@Stas-Buzunko
Copy link

i did 3 things that got it working.

  1. updated node to v11+
  2. added --tls-min-v1.0 flag like this node --tls-min-v1.0 index.js
  3. updated axios to .19

first 2 didn't help without the last one.

@SwiTool
Copy link

SwiTool commented Jun 25, 2019

i did 3 things that got it working.

  1. updated node to v11+
  2. added --tls-min-v1.0 flag like this node --tls-min-v1.0 index.js
  3. updated axios to .19

first 2 didn't help without the last one.

Thank you for that ! Next time i'll work on proxies i'll take a look.

I have been struggling for an entire day trying to make this work, that was in april, but I finally switched to another library.
Proxies work very well with node-fetch combined with https-proxy-agent.

@diegolaciar
Copy link

Axios default.proxy settings don't work on Chrome.
Any workaround for this?

@habmukandu
Copy link

Axios (v.19) is not working with our corporate proxy for https requests. We use a squid proxy and it returns a protocol error in our case.

You can reproduce the error in node (with a public squid proxy) using latest Axios and an Axios version using 'https-proxy-agent':

run:
npm install axios axios-https-proxy-fix

create file 'index.js' with content:

const axiosDefaultConfig = {
    baseURL: 'https://jsonplaceholder.typicode.com/posts',
    proxy: {
        host: '142.93.165.82',
        port: 8080,
        protocol: 'http'
    }
};

const axios = require ('axios').create(axiosDefaultConfig);
axios.get('42')
    .then(function (response) {
        console.log('Response with axios was ok: ' + response.status);
    })
    .catch(function (error) {
        console.log(error);
    });

const axiosFixed = require ('axios-https-proxy-fix').create(axiosDefaultConfig);
axiosFixed.get('42')
    .then(function (response) {
        console.log('Response with axios-https-proxy-fix was ok: ' + response.status);
    })
    .catch(function (error) {
        console.log(error);
    });

run
node index.js

Then you can see in console log that Axios fails with a protocol error while the modified Axios version using https-proxy-agent succeeds.

@kraiz
Copy link

kraiz commented Dec 19, 2019

I was able to make requests to some servers but not all. Seems like it depends on the servers cipher suites. For all servers with just cipher suites beginning with ECDHE_ the request failed.

Following the axios' code we need to make sure that this if expression is false: http.js#L100 . I fixed the problem with this axios configuration (applying it to @habmukandu code above):

const HttpsProxyAgent = require('https-proxy-agent');

const const axiosDefaultConfig = {
    baseURL: 'https://jsonplaceholder.typicode.com/posts',
    proxy: false,
    httpsAgent: new HttpsProxyAgent('http://142.93.165.82:8080')
};

const axios = require ('axios').create(axiosDefaultConfig);
axios.get('42')
    .then(function (response) {
        console.log('Response with axios was ok: ' + response.status);
    })
    .catch(function (error) {
        console.log(error);
    });

tldr: setup httpsAgent and (!) set proxy: false.

@christianp86
Copy link

+1

@williamdarkocode
Copy link

none of these shite solutions work!

@ch-benard
Copy link

@kraiz Tks ! Your solution works for me.

@adam-s
Copy link

adam-s commented Feb 10, 2020

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

@nsharma1989
Copy link

Hi Is there any known solution to this yet.. I have tried a bunch of stuff but they are not working

@adam-s
Copy link

adam-s commented Feb 27, 2020

@nsharma1989 Use node-tunnel to create an agent that will tunnel through all the proxies. Add the agent as a property to the request object configuration. Post an example of the request you are trying to make.

@nsharma1989
Copy link

nsharma1989 commented Feb 27, 2020

Hi @adam-s , Thank you for the prompt response I did tried that too however no success. could you help me pointing to a code snippet may be? Thank you
Also I want to ignore the cert_invalid_errors .

@adam-s
Copy link

adam-s commented Feb 27, 2020

const axios = require('axios');
const tunnel = require('tunnel');

const agent = tunnel.httpsOverHttp({
  proxy: {
     host: 'http://proxy.example.com',
     port: 22225
     proxyAuth: `username:password`,
   },
 });

const responsePromise = axios.request({
  url: 'https://github.com/axios/axios/issues/2072',
  method: 'get',
  headers: {
    'User-Agent': 'Chrome',
 },
  agent,
  port: 443,
});

My proxy is over http while the call to github is over https so I use the appropriate method. I read that with tunnel setting the port which is 443 over https might be required, however, it worked without setting it in my case.

@nsharma1989
Copy link

@adam-s tried the same but my https request still gives t::ERR_CERT_AUTHORITY_INVALID
after adding rejectUnauthorized: false

@adam-s
Copy link

adam-s commented Feb 27, 2020

@nsharma1989 Sounds frustrating. I can't help beyond my own success using node-tunnel with axios. I don't know if a server can force using a signed certificate or not. I was reading about self-signed certificates which need to be shared between the client and server.

I assume you placed rejectUnauthorized: false on the agent configuration

const agent = tunnel.httpsOverHttp({
  proxy: {
     host: 'http://proxy.example.com',
     port: 22225
     proxyAuth: `username:password`,
   },
  rejectUnauthorized: false,
 });

Sorry I can't be more help.

@shasu01
Copy link

shasu01 commented Mar 19, 2020

I have proxy in my package.json and I call my endpoint using axios.get ('/get/GetAllJobs')
On local dev it works, but not after npm run-script build

@chinesedfan
Copy link
Collaborator

Let me make some conclusions,

  • For the original issue, as I answered in stackoverflow, axios's config.proxy is Node.js only. I think it is meaningless in browsers. You can check lib/adapters/xhr.js and will find nothing related to proxy there.
  • For other users discussed proxies in Node.js, there were two main ways.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests