30

It's basically the same question here.

I'm using a browser. The following code is compiled by webpack. I tried this:

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.

I also tried this with the same code, but it did not work:

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

Then, I tried with httpsAgent:

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.

Is this a bug? Am I cursed or maybe do I have a problem reading the documentation?

7
  • Not too familiar with proxies in general let alone with axios, but at a glance should the port really be 80 for an https request? May 6, 2019 at 11:38
  • What ip do you expect to get? Are you aware that propably you are browsing the internet with a single external ip provided by your ISP? What is the result if you remove the proxy thing from axios ?
    – leopal
    May 6, 2019 at 12:41
  • @obermillerk it just works. I tested via an online proxy checker and my proxy is fine, even by going over HTTPS through port 80.
    – Sw0ut
    May 6, 2019 at 18:15
  • 1
    Have you considered using another solution? The Fetch API comes to mind, it’ll probably replace Axios and the likes in the future anyways. May 6, 2019 at 20:33
  • 1
    In that case this answer will be of interest I think: stackoverflow.com/questions/44524236/… May 7, 2019 at 6:45

5 Answers 5

22

if you want to use axios and work-around the issue then consider using https-proxy-agent for proxy agent as mentioned in link

    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});
1
20
+50

There is an open issue on axios's github page.

The issue is labeled as bug from 31 Mar and is not resolved yet.

So it seems you are not cursed, just a bug in axios.

You may add your details to that thread in order to dev team prioritize this issue.

In case you cannot wait for this issue to be resolved, you may consider using fetch API like @Sumi Straessle proposed in the comments.

1
  • 2
    Indeed... I'll X-post what I said here, but I have just tested with node-fetch and in 5 minutes i've been able to do what in didn't succeed to do in 2 days with axios. i'll stick with node-fetch. Thank you for your answer and your time.
    – Sw0ut
    May 7, 2019 at 9:01
3

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.

1
  • @user1974458 It does seem to work. In options: proxy: { host: x.x.x.x, port: xx }
    – Neeraj
    Aug 19, 2021 at 13:18
0

I was able to get it working by doing so

const agent = new HttpsProxyAgent(`http://proxy:port@username:password`);
axios({
   method: 'get',
   httpsAgent: agent,
   url: 'https://api.ipify.org?format=json'
}).then(response => console.log(response.data)).catch(err => console.log(err))

My proxies are https, so when defining your HttpsProxyAgent, make sure to use http over https.

-4

If you are trying to access an HTTPS URL by an HTTP proxy, you need to create an HTTPS-HTTP tunnel.

I found the solution for this issue in this post: https://janmolak.com/node-js-axios-behind-corporate-proxies-8b17a6f31f9d. Now it works perfectly!

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.