node.js - Nodejs 应用程序如何向我的另一个 https 服务器请求?

标签 node.js https request

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

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/ ' }

最佳答案

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

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

关于node.js - Nodejs 应用程序如何向我的另一个 https 服务器请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31610546/

相关文章:

node.js - 如何为 gulp-jasmine 设置环境变量

javascript - 在node.js中将数组对象转换为json

https - Windows 应用商店应用程序使用自签名 SSL 证书连接到 HTTPS

jquery - 在 NodeJS 上使用 jQuery 的函数 css()

java - Play Framework 2.0 - asJson() 总是返回 null

javascript - 事件循环和 v8 引擎在 NodeJS 中如何交互?

node.js - 如何在 browserify 中使用非 commonjs 库?

security - 在不使用 SSL/TLS 的情况下交换数据的方法有哪些

redirect - SSL 证书 - 不受信任的错误

python-3.x - 如何修复错误 urllib.error.HTTPError : HTTP Error 400: BAD REQUEST?