尝试从Node.js执行 POST 请求时出现 400 请求超时错误



以下是详细解释

我已经设置了发布数据

var post_data = JSON.stringify({
"primaryContact": {
"id": 'contact_id'
},
"subject": "subject"
});

设置 https 模块选项

var https_options_post = {
host: hostName,
method: 'POST',
path: "/services/rest/connect/v1.3/incidents",
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data),
'Authorization': auth
}
}

创建了一个处理 POST 请求的函数

function postJSON(https_options_post, callback) {
var request = https.request(https_options_post, function (response) {
console.log('In POST Request Function block');
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
console.log(body);
var bodyJSON = JSON.parse(body);
var result = "Incident Reference Number: " + bodyJSON.lookupName;
callback(null, result);
});
response.on('error', callback);
})
.on('error', callback)
.end();

调用的函数

postJSON(https_options_post, function (err, result) {
if (err) {
return console.log('Error while retrieving Data: ', err);
}
console.log(result);
});

但返回的响应是

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>408 Request Time-out</title>
</head>
<body>
<h1>Request Time-out</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
</body>
</html>

当我使用 REST 客户端扩展在 chrome 中点击 API 并传递基本身份验证参数时,从服务器返回的是 JSON。请帮助并告诉代码中是否有问题。

没有数据写入request流,服务器保留预期的Content-Length字节的数据。在request.end()解决之前写request.write(post_data)

与 Node 的汇合.js示例参考:

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

代码复制 Ref 和完整示例, https://www.snippetbucket.com/confluence-nodejs-rest-api/

相关内容

最新更新