javascript - 在 js 中使用自签名证书发出请求(使用来自 npm 的请求 promise )

标签 javascript node.js ssl request-promise self-signed-certificate

我正在使用 request-promise 发出一个简单的请求库,但它出错了,因为我的链中有一个自签名证书(这是一项要求,没有它不是一个选项)

使用 NPM 我可以指定该证书来安装包(我已经完成了):

npm config set cafile "<path to my certificate file>"

然后我可以安装任何软件包,因为它知道信任我的自签名证书。

有没有办法用request-promise发出请求?并指定要信任的自签名证书?

请求非常简单:return request.get('/myendpoint')

由于自签名证书,它只是抛出一个错误。

如果 request-promise 不可能是否有另一个库支持这种功能?

最佳答案

因为 request-promiserequest 库的包装器(它本身是原生 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/

相关文章:

php - 在 Apache 服务器上使用 OpenSSL 进行单点登录

javascript - iframe - 动态高度/宽度可能吗?

node.js - XCode + react 原生 : "Bundle React Native code and images" build phase taking too long

javascript - 对象结构在内部改变

node.js - nodejs mongo,生成测试数据

Node.js 中 PHP 的 gzuncompress 函数

html - rotativa 不在 SSL 上提供页眉或页脚

ssl - 如何使用 SelfSSL 创建自签名证书

javascript - React-native:tvOS 如何强制聚焦按钮?

javascript - 用于过滤/显示两个单独类别类型的脚本