How to do HTTPS GET with client certificate in node

你。 提交于 2019-12-28 23:17:29

问题


I can use curl for making a GET request ->

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

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

Thanks in advance!


回答1:


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


来源:https://stackoverflow.com/questions/35478215/how-to-do-https-get-with-client-certificate-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!