节点HTTPS请求实际上是HTTP

问题描述:

有人可以告诉我,为什么在此的NodeJS HTTPS请求:节点HTTPS请求实际上是HTTP

var options = { 
     "method": "GET", 
     "hostname": "www.something.com", 
     "port": 443, 
     "path": "/api/v1/method?from=" + dates.startDate + "&to=" + dates.endDate, 
     "headers": { 
      "accept": "application/json", 
      "authorization": 'Basic ' + new Buffer(access.key + ':' + access.secret).toString('base64'), 
      "cache-control": "no-cache" 
     } 
    }; 

    var req = https.request(options, function(res) { 
     var chunks = []; 

     res.on("data", function(chunk) { 
      chunks.push(chunk); 
     }); 

     res.on("end", function() { 
      var body = Buffer.concat(chunks); 
      console.log(body); 
     }); 

     res.on('error', function(e) { 
      console.log(e); 
     }); 
    }) 

    req.end(); 

结束外出HTTP和HTTPS不?在看起来像这样的调试HTTP日志:

“→GET http://www.something.com/api/v1/method?from=2017-01-01&to=2017-01-25

这是工作,我得到的结果,但我会使用HTTPS ...

什么感到相当有它我做错了?

+0

的[默认'protocol'](https://nodejs.org/api/http.html#http_http_request_options_callback)是'” http'';如果你想''https''然后设置它。 – jonrsharpe

尝试改变此:

var options = { 
     "method": "GET", 
     "hostname": "www.something.com", 
     "port": 443, 
     "path": "/api/v1/method?from=" + dates.startDate + "&to=" + dates.endDate, 
     "headers": { 
      "accept": "application/json", 
      "authorization": 'Basic ' + new Buffer(access.key + ':' + access.secret).toString('base64'), 
      "cache-control": "no-cache" 
     } 
    }; 

到:

var options = { 
     "method": "GET", 
     "hostname": "www.something.com", 
     "port": 443, 
     "protocol": "https:", 
     "path": "/api/v1/method?from=" + dates.startDate + "&to=" + dates.endDate, 
     "headers": { 
      "accept": "application/json", 
      "authorization": 'Basic ' + new Buffer(access.key + ':' + access.secret).toString('base64'), 
      "cache-control": "no-cache" 
     } 
    }; 
+0

Jep,就是这样!非常感谢! :) – Seb