在Axios中禁用SSL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Axios中禁用SSL相关的知识,希望对你有一定的参考价值。

有没有一种简单的方法可以在Axios中禁用SSL验证。我试过这个 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; 但它不工作。

下面是我的代码的例子"

const postPosts = () => {
  axios
    .post("https://xxx.dev.lab", {
      Username: "xxx",
      Password: "xxx"
    })
    .then(response => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    });
};
postPosts();
答案

Axios到目前为止还没有解决这种情况--你可以试试。

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

但这是一个非常糟糕的想法,因为它在整个节点服务器上禁用了SSL... ...

或者你可以配置 axios 来使用一个自定义代理,并为该代理设置 rejectUnauthorized 为 false,如这里所提到的那样

例如:

// At instance level
const instance = axios.create({
  httpsAgent: new https.Agent({  
    rejectUnauthorized: false
  })
});

instance.get('https://something.com/foo');

// At request level
 const agent = new https.Agent({  
 rejectUnauthorized: false
});

axios.get('https://something.com/foo', { httpsAgent: agent });

以上是关于在Axios中禁用SSL的主要内容,如果未能解决你的问题,请参考以下文章

SSL 证书 - 在 axios 中禁用验证并做出反应

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

在 Axios 中禁用 JSON 解析

在给定的 java 代码中禁用 SSL 连接