Hanny
Hanny

Reputation: 682

Make request with a self-signed cert in js (using request-promise from npm)

I'm making a simple request using the request-promise library, but it errors out because I have a self-signed cert in my chain (which is a requirement, not having it is not an option)

with NPM I can specify that cert to install packages (which I've done):

npm config set cafile "<path to my certificate file>"

then I can install whatever packages becuase it knows to trust my self-signed cert.

Is there a way to make a request with request-promise and specify the self-signed cert to trust?

The request is very straightforward: return request.get('/myendpoint')

It just throws an error because of the self-signed cert.

If it's not possible with request-promise is there another library that allows that kind of functionality?

Upvotes: 1

Views: 2322

Answers (1)

jakemingolla
jakemingolla

Reputation: 1639

Since request-promise is a wrapper around the request library (which in itself is a wrapper around the native http module), you can add the following option to the request parameters:

const rp = require('request-promise');

const agentOptions = {
  host: 'www.example.com'
, port: '443'
, path: '/'
, rejectUnauthorized: false
};

const agent = new https.Agent(agentOptions);

rp({
  url: "https://www.example.com/api/endpoint",
  method: 'GET',
  agent: agent
}).then(...);

Based off of the http.request documentation.

Upvotes: 1

Related Questions