簡體   English   中英

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

[英]Https request not working in 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'
    }
};

另外,您要使用端口80,通常不是HTTPS端口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM