节点https请求实际上是http

时间:2017-01-29 10:29:22

标签: node.js https httprequest

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

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 ...

我做错了什么?

1 个答案:

答案 0 :(得分: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"
        }
    };