HTTPS请求在Node.js中不起作用 [英] Https request not working in node.js

查看:207
本文介绍了HTTPS请求在Node.js中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的node.js应用中,我想进行https api调用.我正在尝试使用https模块,但是它不起作用,但是随后我尝试使用了request模块,并且可以工作.

In my node.js app, I want to make an https api call. I am trying with the https module and it is not working, but then I try with a request module, and that works.

不起作用

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);
});

我不知道为什么它在第一个上不起作用.有人知道为什么吗?

I can't figure out why it does not work on the first one. Does anyone know why?

谢谢

推荐答案

EDIT 也切换到端口443.

EDIT Switched to port 443 as well.

您的host似乎包含路径的一部分?尝试以下操作(将主机保留在host中,并将路径移至path):

Your host seemed to include part of the path? Try this instead (left just the host in host and moved the path to path):

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

这篇关于HTTPS请求在Node.js中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆