【问题标题】:Node https request is actually http节点https请求其实是http
【发布时间】:2017-01-29 10:29:22
【问题描述】:

有人可以告诉我,为什么 nodejs 中有这个 https 请求:

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 的形式输出?在 debug-http 日志中看起来像这样:

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

它正在工作,我确实得到了结果,但我宁愿使用 https...

我做错了什么?

【问题讨论】:

标签: node.js https httprequest


【解决方案1】:

尝试改变这个:

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"
        }
    };

【讨论】:

  • 杰普,就是这样!非常感谢! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多