javascript - FetchError:无法验证第一个证书,但我添加了rejectUnauthorized:false
问题描述
我有一个getServerSideProps()
调用外部 API 但抛出此错误的简单函数:
FetchError:对 https://nginx/api/items 的请求失败,原因:无法验证第一个证书
Node 服务器不信任我的自签名证书。
所以我发现这篇 Stack Overflow 帖子绕过了它(我只在开发中使用它):
所以我rejectUnauthorized: false
像这样添加到我的 Axios 调用中:
export async function getServerSideProps() {
const res = await fetch('https://nginx/api/items',
{ rejectUnauthorized: false,
method: 'GET',
}
)
const { data } = await res.json()
return { props: { data } }
}
但我仍然得到错误。
还有其他方法可以让我的自签名证书与 Next 一起使用吗?我找到了其他一些解决方案,但它们是针对 Express 的,我不知道如何使用 Next.js 为 Node 实现它
解决方案
rejectUnautorized
属于HttpAgent
:_
const https = require('https');
const agent = new https.Agent({
rejectUnauthorized: false
});
const res = await fetch('https://nginx/api/items', {
method: 'GET',
agent
}
);
推荐阅读
- c# - 如何在c#中使用Zxing安排二维码的定位
- sql - 如何显示与成员对应的指定角色
- python - 如何将字典转换为键值为新列的数组
- swift - 复制或分配时,数组如何快速深度复制自身
- javascript - 如何使用 DataTables 获取行值和删除数据?
- google-app-engine - Stackdriver Trace 与 Google App Engine Go 1.11 运行时
- ios - RxSwift RetryWhen 导致可重入异常
- graphql - 从 commitMutation 的更新程序函数更新存储时出现问题
- python - 如何在网站中使用json数据,我应该使用什么样的数据库?
- logging - 如何在烧瓶视图中使用应用程序日志记录,它返回无法导入名称记录器