Seb
Seb

Reputation: 1113

Node https request is actually http

Can somebody please tell me, why this https request in 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();

Ends up going out as http and not https? In the debug-http logs in looks like this:

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

It is working and I do get results, but I would rather have it using https...

What am I doing wrong?

Upvotes: 1

Views: 566

Answers (1)

rsp
rsp

Reputation: 111336

Try changing this:

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

to:

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

Upvotes: 1

Related Questions