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

查看:43
本文介绍了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天全站免登陆