+-
在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();
0
投票

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