'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?
Solution 1:[1]
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});
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | aravind_reddy |
Solution 2 | chinesedfan |