如何在节点中使用客户端证书进行 HTTPS GET [英] How to do HTTPS GET with client certificate in node

查看:24
本文介绍了如何在节点中使用客户端证书进行 HTTPS GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 curl 发出 GET 请求 ->

I can use curl for making a GET request ->

`curl -v https://example.com:82/v1/api?a=b` -E client_cert.pem:password 

如何在 node.js 中使用相同的内容?我尝试过请求、超级代理但无法通过证书.

How can I use same in node. I tried request, superagent but not able to pass certificate.

提前致谢!

推荐答案

这对我有用 -

var https = require('https');
var fs  = require('fs');

var options = {
  hostname: 'example.com',
  port: 83,
  path: '/v1/api?a=b',
  method: 'GET',
  key: fs.readFileSync('/path/to/private-key/key.pem'),
  cert: fs.readFileSync('/path/to/certificate/client_cert.pem'),  
  passphrase: 'password'
};

var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
  process.stdout.write(d);
  });
});

req.end()

这篇关于如何在节点中使用客户端证书进行 HTTPS GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆