wao3

在 node.js 中使用 axios 时,有时需要忽略 SSL 证书,在百度搜半天都搜不到,最后在 axios 的 github issue 中找到了解决办法。

需要注意本文介绍的是在 node.js 服务端中使用 axios,不是在前端使用,前端由于网络操作都要经过浏览器,浏览器自身要做安全上的保障,所以不能忽略证书

首先需要在项目中使用以下命令导入httpsaxios依赖:

npm i https
npm i axios

然后可以根据不同的需求编写代码:

const https = require(\'https\');
const axios = require(\'axios\');

// 创建忽略 SSL 的 axios 实例
const ignoreSSL = axios.create({
  httpsAgent: new https.Agent({  
    rejectUnauthorized: false
  })
});
ignoreSSL.get(\'https://something.com/foo\');

// 在 axios 请求时,选择性忽略 SSL
const agent = new https.Agent({  
  rejectUnauthorized: false
});
axios.get(\'https://something.com/foo\', { httpsAgent: agent });

分类:

技术点:

相关文章:

  • 2021-06-19
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-08-13
  • 2021-09-29
  • 2022-12-23
猜你喜欢
  • 2021-12-15
  • 2021-09-18
  • 2022-12-23
  • 2021-10-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
相关资源
相似解决方案