繁体   English   中英

尝试从node.js发出POST请求时遇到400 Request TimeOut错误

[英]Getting 400 Request TimeOut error on trying to do a POST request from node.js

以下是详细说明

我已经设置了帖子数据

 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并传递基本的auth参数时,服务器返回的就是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);
});

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM