在我的 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);
});
我不明白为什么它在第一个上不起作用。有谁知道为什么?
谢谢
最佳答案
编辑也切换到端口 443。
<小时/>您的主机
似乎包含部分路径?请尝试以下操作(仅将主机保留在 host
中,并将路径移至 path
):
var options = {
host : 'myserver',
port : 443,
path : '/platform-api/v1/projects?access_token=38472',
method : 'GET',
headers : {
'Accept' : 'application/json'
}
};
关于javascript - https 请求在 Node.js 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46477689/