Error CERT_UNTRUSTED while executing https request

I am trying to execute https request from my server side of node, but this gives me the following error: -

 Caught exception: Error: CERT_UNTRUSTED 

If I execute the http request , then it works fine, but for https links it doesn't work .

Here is my code: -

 var request = require('request'); request('https://en.m.wikipedia.org/wiki/Astrid_Olofsdotter_of_Sweden', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Show the HTML for the Google homepage. } }) 

Any idea?

+5
source share
4 answers

It could be up to a few things, ive run your code and its fine for me

 node --version v0.12.5 

so i would look at

  • the version of nodejs you are using

root certificates are periodically updated, I suggest updating, because it is possible that the certificate used is newer than the root certificates in your distribution, and therefore shows as unreliable

  1. network - it’s possible that you are behind a proxy server that is doing something unexpected with your requests.

  2. target server, it may return something unexpected.

Most likely, 1, that you are using a version of nodejs that does not recognize the site certificate provider and therefore says that it is unreliable.

I would put 2 and 3 at about the same level of probability. if you are spidering wikipedia, they may have blocked you / clicked on the error page where the certificate may be invalid.

While you, like other posters, could turn off verification, I would advise against it as a habit. And never do this in a production environment.

The only time I break this rule is self-signed certificates on local machines.

+3
source

This seems like a problem with SSL, the best way to fix SSL in a network environment.


Now you can work around the problem by making rejectUnauthorized false

 const request = require("request"); const https = require('https'); const agentOptions = { host: 'en.m.wikipedia.org', port: '443', path: '/wiki/Astrid_Olofsdotter_of_Sweden', rejectUnauthorized: false }; const agent = new https.Agent(agentOptions); const postOption = { method: 'GET', agent: agent, uri: 'https://en.m.wikipedia.org/wiki/Astrid_Olofsdotter_of_Sweden' }; request(postOption, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body); } }); 
0
source

A very close answer is here .

A very detailed and specific answer to this question is given here .

I would recommend the following:

Before calling https.request() set:

 process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 

In the agent settings, set:

 const agentOptions = { ... rejectUnauthorized: false }; 

Also refer to the documentation for the TLS / SSL request library .

0
source

You can bypass https with the following commands:

 npm config set strict-ssl false 

Setting strict-ssl false allows all node servers to access any URL.

or set the registry URL with https or http, as shown below:

 npm config set registry="http://registry.npmjs.org/" 

Note: However, I believe that https bypass is not a real solution, but we can use it as a workaround.

Hope this helps.

-1
source

Source: https://habr.com/ru/post/1262065/


All Articles