Javascript 请求,错误:无法验证第一个证书

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36194326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:44:26  来源:igfitidea点击:

Request, Error: unable to verify the first certificate

javascriptnode.js

提问by Mr.D

I wanted to make simple POST HTTP request by using requestmodule:

我想通过使用request模块发出简单的 POST HTTP 请求:

var request = require("request");
var form = {form: {some: "form", attributes: "attrs"}}
request.post("https://example.com", form)
   .on('response', function(response) {

   if (response.statusCode === 200) {
     console.log("DONE");
   } else {
     console.log("FAIL");
   }
});

When I launch this code it throws me this error message:

当我启动此代码时,它会向我抛出此错误消息:

Error: unable to verify the first certificate
at Error (native)
at TLSSocket.<anonymous> (_tls_wrap.js:1057:38)
at emitNone (events.js:67:13)
at TLSSocket.emit (events.js:166:7)
at TLSSocket._finishInit (_tls_wrap.js:596:8)

I think this is happening because url has https, but I don't know how to fix this error.

我认为这是因为 url 有https,但我不知道如何解决这个错误。

How to disable checking certificate?

如何禁用检查证书?

回答by michelem

Add "rejectUnauthorized": falseas option:

添加"rejectUnauthorized": false为选项:

request.post({url: "https://example.com", "rejectUnauthorized": false}, form)
   .on('response', function(response) {

   if (response.statusCode === 200) {
     console.log("DONE");
   } else {
     console.log("FAIL");
   }
});

Or add the appropiate certificate via https://www.npmjs.com/package/ssl-root-cas

或者通过https://www.npmjs.com/package/ssl-root-cas添加合适的证书

require('ssl-root-cas').inject();