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

时间:2016-03-24 06:20:52

标签: javascript node.js

我想使用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");
   }
});

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

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)

我认为这是因为网址有https,但我不知道如何修复此错误。

如何禁用检查证书?

2 个答案:

答案 0 :(得分:5)

添加"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");
   }
});

或通过https://www.npmjs.com/package/ssl-root-cas

添加适当的证书
require('ssl-root-cas').inject();

答案 1 :(得分:1)

您可以尝试在“文件”->“设置”下禁用ssl证书验证 Settings image