How do I use the node.js request module to make an SSL call with my own certificate?

This largely elaborates on Peter Lyons’ answer, providing an example.

I am assuming that you are requesting a domain running over HTTPS with a certificate signed by your own certificate authority (ca).

When using the request library, as you do, there is no need to actually instantiate the agent yourself, you can simply provide some agentOptions to the request you are making. The following is an example:

request({
  method: "POST",
  uri: "https://localhost/entries",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    name: "someEntry"
  }),
  agentOptions: {
    ca: fs.readFileSync("certs/ca.cert.pem")
  }
}, function(error, httpResponse, body) {
  //handle response
});

The important thing here is the agentOptions, which you provide the certificate of a ca. All domains using certificates signed by the ca are now accepted. Imagine a ca CA1 has signed three domains, D1, D2, D3. Setting the ca to CA1 results in allowing requests to all of the domains D1, D2, D3 (but not D4 signed by a different ca).

Point being: the "certs/ca.cert.pem" must be the certificate of the signing certificate authority.

Leave a Comment