15

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 Answer 1

21

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()
4
  • What is the key.pem file? Sep 27, 2018 at 17:26
  • @KingJulien key.pem is private key needed to have authentication with host AFAIR :) Nov 15, 2018 at 13:11
  • the private key is not needed for an http request. it is used by the server Aug 16, 2021 at 23:51
  • 6
    @youneszeboudj it can be used on client when you use a client certificate to authenticate your https request
    – TMG
    Aug 17, 2021 at 20:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.