Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use axios with a proxy server to make an https call?

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?

like image 270
Sw0ut Avatar asked May 04 '19 09:05

Sw0ut


People also ask

Does Axios work with proxy?

Axios like barely at all. But the main thing is you have to use this HTTPS proxy agent package. I'll link to that in the comments or the description. And then you just pass in the URL of your proxy with credentials.

Does Axios work https?

You can make a POST request using Axios to “post” data to a given endpoint and trigger events. To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server.


1 Answers

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.

like image 94
leopal Avatar answered Oct 19 '22 04:10

leopal