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

问题描述 投票:5回答:1

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

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

我怎样才能在节点中使用它。我试过请求,superagent但不能通过证书。

提前致谢!

node.js ssl curl npm certificate
1个回答
7
投票

这对我有用 -

var https = request('https');

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()
© www.soinside.com 2019 - 2024. All rights reserved.