背景
最近在 Vite 项目需要模拟远程代理接口,插件需要在configureServer
钩子的server
中间件,使用axios
请求https://
协议接口
错误信息
Error: unsupported certificate purpose
错误原因
请求https
协议接口需要证书
解决方法
构造证书,再进行请求
import axios from 'axios';
import https from 'https';
axios.get(htmlUrl, {
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
});
参考资料:axios
httpAgent
andhttpsAgent
define a custom agent to be used when performing http and https requests, respectively, in node.js.