Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Rest call to a server that requires a certificate

How do a make a rest call from Node.js to a server that requires a certificate for authentication?

like image 336
micc0 Avatar asked Oct 24 '25 04:10

micc0


1 Answers

The solution is to use a custom connection pool, for which you'll need to use a custom Agent.


Here's an example using the standard https module, straight from the documentation:

var options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET',
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
options.agent = new https.Agent(options);

var req = https.request(options, function(res) {
  ...
}

And if you use mikeal's request, you can set the custom agent in the pool option.

like image 194
Paul Mougel Avatar answered Oct 25 '25 18:10

Paul Mougel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!