Is there a way to make a rest call that requires a client certificate for authentication through Node.js ?
Asked
Active
Viewed 1.6k times
1 Answers
18
Yes, you can do that quite simply, here done using a regular https request;
var https = require('https'), // Module for https
fs = require('fs'); // Required to read certs and keys
var options = {
key: fs.readFileSync('ssl/client.key'), // Secret client key
cert: fs.readFileSync('ssl/client.crt'), // Public client key
// rejectUnauthorized: false, // Used for self signed server
host: "rest.localhost", // Server hostname
port: 8443 // Server port
};
callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
https.request(options, callback).end();

Joachim Isaksson
- 176,943
- 25
- 281
- 294
-
1How do i pass a JSON or XML in the request body if i use this approach ? – Anand Divakaran May 10 '14 at 14:23
-
1@AnandDivakaran If you need the full code for a post request, [this answer](http://stackoverflow.com/a/6158966/477878) shows how to do that. In this case, you'll only need to replace `http` with `https` and add the key and cert options shown above to the `post_options` for client certificates to work. – Joachim Isaksson May 10 '14 at 14:54
-
Isn't there a library which handles client-side certificates? I was using [node-rest-client](https://www.npmjs.com/package/node-rest-client) but ran into this need and it doesn't seem to support that. – Chloe Nov 01 '19 at 02:37