如何在nodejs中关闭curl对证书的验证?

时间:2013-09-26 09:11:47

标签: node.js curl https

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

  

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

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

感谢您的支持。

2 个答案:

答案 0 :(得分:1)

rejectUnauthorized option设置为false

var https = require('https');

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

答案 1 :(得分:0)

检查以下代码:

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();

<小时/> 根据评论更新
在上面的代码中,我更改了https&amp; 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