[英]Getting 400 Request TimeOut error on trying to do a POST request from node.js
Below is the explanation in detail 以下是详细说明
I have setup the post data 我已经设置了帖子数据
var post_data = JSON.stringify({ "primaryContact": { "id": 'contact_id' }, "subject": "subject" });
Setup the https module options 设置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 } }
Created a function which handles POST request 创建一个处理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();
Called the function 称为功能
postJSON(https_options_post, function (err, result) { if (err) { return console.log('Error while retrieving Data: ', err); } console.log(result); });
But the returned response is 但是返回的响应是
<!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>
When I hit the API in the chrome using REST client extension and pass on the basic auth parameters the return from server is JSON. 当我使用REST客户端扩展在Chrome中访问API并传递基本的auth参数时,服务器返回的就是JSON。 Please help and tell if something wrong in the code.
请帮助并告诉代码中是否有错误。
There is no data being written into the request
stream and the server keeps expects Content-Length
bytes of data. 没有数据写入
request
流,并且服务器保持期望的Content-Length
数据字节。 Writing request.write(post_data)
before request.end()
solved it 在
request.end()
解决之前写入request.write(post_data)
Confluence with Node.js Example Reference: 与Node.js示例参考的融合:
reqGet.write(jsonObject)
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
Code copied Ref & full example on, https://www.snippetbucket.com/confluence-nodejs-rest-api/ 在https://www.snippetbucket.com/confluence-nodejs-rest-api/上复制了Ref和完整示例的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.