Thursday 1 June 2017

Using HTTPS Client Cert & Key with Node

I'm trying to get client certificates working for talking to my api server. With curl it works fine.

nodejs@ip-10-1-59-225:/home/foouser$ curl --key /var/run/keys/key.pem --cert /var/run/certs/cert.pem -k -v https://api.myserver.com

However if I try the same example with node it doesn't.

const options = {
  hostname: 'api.myserver.com',
  port: 443,
  path: '/',
  method: 'GET',
  rejectUnauthorized: false,
  key: fs.readFileSync('/var/run/keys/key.pem'),
  cert: fs.readFileSync('/var/run/certs/cert.pem')
};

const req = https.request(options, (res) => {
  res.on('data', (data) => console.log(data));
});

req.on('error', (err) => console.log(err));

Which gives this error

{ Error: socket hang up
    at TLSSocket.onHangUp (_tls_wrap.js:1124:19)
    at TLSSocket.g (events.js:292:16)
    at emitNone (events.js:91:20)
    at TLSSocket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNRESET' }

Any suggestions on what might be wrong?



via Mike

No comments:

Post a Comment