【问题标题】:node.js request module giving error for http call [Error: Invalid protocol: null]node.js 请求模块为 http 调用提供错误 [错误:无效协议:null]
【发布时间】:2016-01-23 14:56:39
【问题描述】:

我正在使用 node.js 的请求模块对具有如下地址的 api 进行 HTTP GET 调用:http://x.x.xxx.xx:8000/names 但调用返回错误[Error: Invalid protocol: null]

我的 api 调用如下所示:

request.get({
    uri: 'http://x.x.xxx.xx:8000/names'
}, function(err, res, body){
    if(err == null){
        res.status(200).send(data);
    }else{
        console.log(err);
    }});

注意:我通过 "npm config set proxy" 和 "npm config set https proxy" 为 npm 使用了代理。删除它们也不能解决问题。

谢谢

【问题讨论】:

  • 请向我们展示您的代理配置。

标签: node.js proxy request


【解决方案1】:

既然这样,工作得很好,我会说问题出在你的代理配置中。

request.get({
    uri: 'http://localhost:8000/names'
}, function(err, res, body){

});

试试这个:

npm config set proxy http://usr:pwd@host:port

【讨论】:

    【解决方案2】:

    使用request.get,如果你想传递一个选项,你必须首先将url指定为一个字符串,然后是一个带有选项的字典。该错误可能是由于您在字典中传递的混淆。尝试使用带有字典参数的request()request.get('http://xx.xx.xx.xx:8080/')

    参考文档:https://www.npmjs.com/package/request#convenience-methods

    【讨论】:

    • 不是这样,这个请求格式正确。
    【解决方案3】:

    我的HTTP_PROXY 环境变量设置不正确

    【讨论】:

      【解决方案4】:

      如果您要连接到 PHP 开发服务器,请see this thread 寻求解决方案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-11
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 2018-06-15
        相关资源
        最近更新 更多