如何使用带有代理服务器的 axios 进行 https 调用? [英] How to use axios with a proxy server to make an https call?

查看:30
本文介绍了如何使用带有代理服务器的 axios 进行 https 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是同样的问题.

我正在使用浏览器.以下代码由 webpack 编译.我试过这个:

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

然后,我尝试使用 httpsAgent:

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?

推荐答案

有一个未解决的问题axios 的 github 页面.

该问题自 3 月 31 日起被标记为错误,尚未解决.

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

看来你没有被诅咒,只是axios的一个bug.

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.

如果您等不及这个问题得到解决,您可以考虑使用 fetch API 就像@Sumi Straessle 在评论中提出的一样.

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

这篇关于如何使用带有代理服务器的 axios 进行 https 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆