Fork me on GitHub

node中使用axios时:Error: unable to verify the first certificate 报错


参考

https://www.daozhao.com/10611.html

报错原因:

  在使用浏览器访问时,客户端、服务器在握手阶段完成验证。当我们在node中使用axios请求时,客户端没法确认服务端的TLS证书

解决方案

1、局部

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

// 在 axios 请求时,选择性忽略 SSL
const agent = new https.Agent({
  rejectUnauthorized: false
})

然后在需要的axios请求中加入option配置httpsAgent: agent

2、全局

const https = require("https");
const axios = require("axios");

// 直接在axios实例上进行配置
const myAxios = axios.create({
  httpsAgent: new https.Agent({
    rejectUnauthorized: false,
  }),
});

然后后续使用myAxios.get、myAxios.post等发起请求的时候就不发生报错了。

posted @   Lencamo  阅读(1306)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示