【问题标题】:How turn off curl's verification of the certificate in nodejs?nodejs中如何关闭curl对证书的验证?
【发布时间】:2025-12-26 18:30:12
【问题描述】:

如果我发送curl -k "https://abc.com?querystring"

-k 选项关闭 curl 对证书的验证

如果我想发出 GET 请求,如何在 nodejs 中执行此操作? 如何以同样的方式覆盖所有的http GET请求?

感谢您的支持。

【问题讨论】:

    标签: node.js curl https


    【解决方案1】:

    rejectUnauthorized option 设置为false

    var https = require('https');
    
    var req = https.request({
        hostname: 'example.com',
        port: 443,
        path: '/',
        method: 'GET',
        rejectUnauthorized: false
    }, function() { ... });
    

    【讨论】:

      【解决方案2】:

      检查以下代码:

      var http = require('http');
      
      var target = {    
          host : 'localhost',
          port : 3000,
          path : 'URL'
          //pfx: Certificate, Private key and CA certificates to use for SSL. Default is null.
          //cert: Public x509 certificate to use. Default null.
      };
      
      var Req_options = { 
          host: target.host,
          port: target.port,
          path: target.path,
          agent: false,
          method: 'GET'
      };
      
      callback = function(response) {
       var str = ''
        response.on('data', function (chunk) {
          str += chunk;
        });
      
        response.on('end', function () {
          console.log(str);
        });
      }
      
      var req = http.request(Req_options, callback);
      req.end();
      


      根据 cmets 更新
      在上面的代码中,我只更改了https & target 如下:
      var https = require('https'); 
      
      var target = {    
          host : 'www.google.co.in',
          port : 443,
          path : '/'
      };
      

      输出如下:

       </html>
          .........
          .........
          .........
          attachEvent&&window.attachEvent("onload",n);google.timers.load.t.prt=e=(new Date).getTime();})();
      </script></body></html>
      

      欲了解更多信息,请查看Node.js API docs

      【讨论】:

      • 我收到错误:UNABLE_TO_VERIFY_LEAF_SIGNATURE