简体   繁体   English

如何在节点中使用客户端证书进行 HTTPS GET

[英]How to do HTTPS GET with client certificate in node

I can use curl for making a GET request ->我可以使用 curl 发出 GET 请求 ->

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

How can I use same in node.如何在 node.js 中使用相同的内容? I tried request, superagent but not able to pass certificate.我尝试过请求、超级代理但无法通过证书。

Thanks in advance!提前致谢!

This worked for me -这对我有用-

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM