有人可以告诉我,为什么在 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...
我做错了什么?
最佳答案
尝试更改此设置:
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"
}
};
关于node.js - Node https请求实际上是http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41919879/