HTTPS请求在Node.js中不起作用

欧米茄

在我的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似乎包括这条路的一部分?请尝试以下操作(将主机留在其中,host然后将路径移至path):

var options = {
    host : 'myserver',
    port : 443,
    path : '/platform-api/v1/projects?access_token=38472',
    method : 'GET',
    headers : {
        'Accept' : 'application/json'
    }
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章