How to do HTTPS GET with client certificate in node

26,238

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()
Share:
26,238
rohitkadam19
Author by

rohitkadam19

Software QA Engineer

Updated on July 09, 2022

Comments

  • rohitkadam19
    rohitkadam19 almost 2 years

    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!

  • King Julien
    King Julien over 5 years
    What is the key.pem file?
  • rohitkadam19
    rohitkadam19 over 5 years
    @KingJulien key.pem is private key needed to have authentication with host AFAIR :)
  • younes zeboudj
    younes zeboudj over 2 years
    the private key is not needed for an http request. it is used by the server
  • TMG
    TMG over 2 years
    @youneszeboudj it can be used on client when you use a client certificate to authenticate your https request