我正在使用 request-promise
发出一个简单的请求库,但它出错了,因为我的链中有一个自签名证书(这是一项要求,没有它不是一个选项)
使用 NPM 我可以指定该证书来安装包(我已经完成了):
npm config set cafile "<path to my certificate file>"
然后我可以安装任何软件包,因为它知道信任我的自签名证书。
有没有办法用request-promise
发出请求?并指定要信任的自签名证书?
请求非常简单:return request.get('/myendpoint')
由于自签名证书,它只是抛出一个错误。
如果 request-promise
不可能是否有另一个库支持这种功能?
最佳答案
因为 request-promise
是 request
库的包装器(它本身是原生 http
模块的包装器),你可以在请求参数中加入如下选项:
const rp = require('request-promise');
const agentOptions = {
host: 'www.example.com'
, port: '443'
, path: '/'
, rejectUnauthorized: false
};
const agent = new https.Agent(agentOptions);
rp({
url: "https://www.example.com/api/endpoint",
method: 'GET',
agent: agent
}).then(...);
基于 http.request
文档。
关于javascript - 在 js 中使用自签名证书发出请求(使用来自 npm 的请求 promise ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56294707/