5

axios请求https网站报错Unable to verify the first certificate

 2 years ago
source link: https://www.daozhao.com/10611.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

axios请求https网站报错Unable to verify the first certificate

如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!

axios请求https网站报错Unable to verify the first certificate

今天在使用axios出现了一个错误Unable to verify the first certificate,就感觉是证书的问题,自己手动在浏览器中输入https的网站没问题啊,但是在用axios请求的时候还是出问题了,最后之后看看能不能屏蔽掉这个检测了。

可以采用下列任意一个方法解决

  • 方法一:直接使用忽略了ssl检测的axios实例
const https = require("https");
const axios = require("axios");
const ignoreSSLAxios = axios.create({
  httpsAgent: new https.Agent({
    rejectUnauthorized: false,
  }),
});

ignoreSSLAxios.get("http://example.com/getData.do", { httpsAgent: agent }).then((res) => res.data);

以后直接用实例ignoreSSLAxios发送请求了

  • 方法二:单次请求的时候忽略ssl检测
// 在 axios 请求时,选择性忽略 SSL
const https = require("https");
const axios = require("axios");
const agent = new https.Agent({
  rejectUnauthorized: false,
});

axios.get("http://example.com/getData.do", { httpsAgent: agent }).then((res) => res.data);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK