Janmajay
Janmajay

Reputation: 49

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

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

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. Please help and tell if something wrong in the code.

Upvotes: 0

Views: 1886

Answers (2)

Tejas Tank
Tejas Tank

Reputation: 1216

Confluence with Node.js Example Reference:

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/

Upvotes: 0

Malice
Malice

Reputation: 1482

There is no data being written into the request stream and the server keeps expects Content-Length bytes of data. Writing request.write(post_data) before request.end() solved it

Upvotes: 1

Related Questions