a blog for those who code

Thursday, 24 March 2016

[Solved] Error: unable to verify the first certificate in Node.js

In this post we will be discussing about how to solve unable to verify the first certificate when doing a post request in Nodejs. Lets say you were using Request module and trying to make https calls, you might end up getting the error "unable to verify the first certificate" because you have not added an appropriate root certificate or you have not added a rejectUnauthorised flag.

Lets take an example :

var request = require("request");
var form = { a: "hello" };
request.post("https://xyz.com", form)
  .on('response', function(response) {
    if(response.statsusCode === 200) {
// Do Something
    }
  }); 

In the above code we are trying to do a post to some https website. If we run this code we will get this error "Error : unable to verify the first certificate".


To fix this issue we will add an appropriate root certificate. ssl-root-cas is a module which will solve node's SSL woes when including a custom certificate. You just need to add require('ssl-root-cas').inject() to your entry point. After this you will not see this error.


If you are trying to do the same thing in your trusted websites or say in your intranet, you can also use the flag rejectUnauthorized and make it false. This will turn off the SLL validation checking but it is not a good idea.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

2 comments:

  1. where is the 'entry point' in the code example above? can you please show the code for the finished working example?

    ReplyDelete
  2. I tried this workaround, did not work for me with EWS example

    ReplyDelete

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License