【发布时间】:2018-03-10 16:50:22
【问题描述】:
在我的 node.js 应用程序中,我想进行 https api 调用。我正在尝试使用 https 模块,但它无法正常工作,但随后我尝试使用 request 模块,并且成功了。
不工作
var options = {
host : 'myserver/platform-api/v1',
port : 80,
path : '/projects?access_token=38472',
method : 'GET',
headers : {
'Accept' : 'application/json'
}
};
var req = https.request(options, function(res) {
res.on('data', function(chunk) {
console.log(chunk);
});
});
req.on('error', function(e) {
console.log(e);
console.log('problem with request:', e.message);
});
req.end();
我明白了
problem with request: getaddrinfo ENOTFOUND myserver/platform-api/v1
myserver/platform-api/v1:80
这行得通
request("https://myserver/platform-api/v1/projects?access_token=38472", function(error, response, body) {
if (error) return console.log(error);
//console.log(error);
//console.log(response);
console.log(body);
});
我不明白为什么它不适用于第一个。有谁知道为什么?
谢谢
【问题讨论】:
-
不是将选项作为参数传递,而是传递 options.path?
标签: javascript node.js api http https