【问题标题】:How nodejs app to request to my another https server?nodejs 应用程序如何向我的另一个 https 服务器请求?
【发布时间】:2015-07-24 12:27:12
【问题描述】:

下面是 nodejs 应用程序请求到我的另一个 https 服务器

var https = require('https');

jsonObject = JSON.stringify({"arg1":"4","arg2":"True"});

// prepare the header
var postheaders = {
    'Content-Type' : 'application/json',
    'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
};

// the post options
var optionspost = {
    host : 'https://www.example.com/',
    path : '/my/path/?arg1=4&arg2=True',
    method : 'POST',
    headers : postheaders
};

var reqPost = https.request(optionspost, function(res) {
    console.log("statusCode: ", res.statusCode);

    res.on('data', function(d) {
        console.info('POST result:\n');
        process.stdout.write(d);
        console.info('\n\nPOST completed');
    });
});

reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
    console.error(e);
});

我总是遇到以下错误,任何人都可以说出我哪里出错了

{ [错误:getaddrinfo ENOTFOUND https://www.example.com/] 代码:'ENOTFOUND', 错误号:'ENOTFOUND', 系统调用:'getaddrinfo', 主机名:'https://www.example.com/'}

【问题讨论】:

    标签: node.js https request


    【解决方案1】:
    // the post options
    var optionspost = {
        host : 'www.example.com',
        path : '/my/path/?arg1=4&arg2=True',
        method : 'POST',
        headers : postheader
    }
    

    主机属性需要是完全限定的域名。删除 https:// 和尾部斜杠,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多