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?

Sakib Arifin
  • 255
  • 2
  • 13
Sw0ut
  • 741
  • 2
  • 9
  • 29
  • 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? – obermillerk May 06 '19 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 06 '19 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 06 '19 at 18:15
  • @leopal i'm expecting the IP to be the proxy IP and not my personal internet providers' IP. If I remove the proxy from axios, it's exactly the same. I asked a friend, he told me that axios was in trouble to handle proxies. But why did they document the proxy things if they do not work? – Sw0ut May 06 '19 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. – Sumi Straessle May 06 '19 at 20:33
  • @SumiStraessle no but now, yes. I'll check this today and even it does not answer the real question, i'll ask you to post your comment as an answer, if the fetch API works with proxies. I'll keep you updated – Sw0ut May 07 '19 at 06:30
  • 1
    In that case this answer will be of interest I think: https://stackoverflow.com/questions/44524236/using-proxy-like-fiddler-with-fetch-api – Sumi Straessle May 07 '19 at 06:45

5 Answers5

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});
aravind_reddy
  • 5,236
  • 4
  • 23
  • 39
20

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.

leopal
  • 4,711
  • 1
  • 25
  • 35
  • 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 07 '19 at 09: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.

chinesedfan
  • 259
  • 1
  • 2
  • 9
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.

Harsha Murupudi
  • 579
  • 1
  • 6
  • 19
BerenBoden
  • 41
  • 5
-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
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – Sabito stands with Ukraine Oct 15 '20 at 13:27