下面是向我的另一个 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/