Ganesh Karamala
Ganesh Karamala

Reputation: 513

'request' : Error: { Error: self signed certificate in certificate chain

I'm trying to fetch some information from below mentioned API and I'm getting below error

const request = require('request');


var options = {
    url : 'https://example.com',
}

OR

var options = {
  url : 'https://example.com',
  agentOptions: {
    ca: fs.readFileSync('ca.pem')
  }
}

console.log(options)

request(options, function (error, response, body) {
  console.log('error:', error); 
  console.log('statusCode:', response && response.statusCode); 
  console.log('body:', body ); 
});



  error: { Error: self signed certificate in certificate chain
        at Error (native)
        at TLSSocket.<anonymous> (_tls_wrap.js:1092:38)
        at emitNone (events.js:86:13)
        at TLSSocket.emit (events.js:185:7)
        at TLSSocket._finishInit (_tls_wrap.js:610:8)
        at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) code: 'SELF_SIGNED_CERT_IN_CHAIN' }

sample pem file

-----BEGIN CERTIFICATE-----
Key
-----END CERTIFICATE-----

I don't want to use either NODE_TLS_REJECT_UNAUTHORIZED='0' or rejectUnauthorized: false

Upvotes: 2

Views: 16413

Answers (2)

Daud Ahmed
Daud Ahmed

Reputation: 328

This will work fine hopefully:

  var options = {
      url : 'https://example.com',
      ca: fs.readFileSync('ca.pem'),
      insecure: true,
      rejectUnauthorized: false,
  }

  console.log(options)

  request(options, function (error, response, body) {
      console.log('error:', error); 
      console.log('statusCode:', response && response.statusCode); 
      console.log('body:', body ); 
  });

Upvotes: 0

robertklep
robertklep

Reputation: 203519

As per the fine manual:

It is possible to accept other certificates than those signed by generally allowed Certificate Authorities (CAs). This can be useful, for example, when using self-signed certificates. To require a different root certificate, you can specify the signing CA by adding the contents of the CA's certificate file to the agentOptions. The certificate the domain presents must be signed by the root certificate specified:

var options = {
  url : 'https://example.com',
  agentOptions: {
    ca: fs.readFileSync('ca.pem')
  }
}

Upvotes: 1

Related Questions