简体   繁体   中英

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?

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.

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

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.

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!

If you are using MERN stack application, try this

Express server is listening at port 5000 In package.json of your React App

add `"proxy": "http://localhost:5000/" like this

 "scripts": {
    .....
    ....
  },

  "proxy": "http://localhost:5000/"
  ,

  "eslintConfig": {
    .....
    ...
  },

While making requests to you API, for example fetching a User's Data

const fetchUser = (userId) => {
    
     const res = await axios.get(`/api/users/${userId}`)
     console.log(res.data)
}

PS: Do not forget / in /api/users.... , this made me cry for days. Such a silly thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM