Node: http.request post method not responding with no error on callback

Created on 1 Nov 2016  路  3Comments  路  Source: nodejs/node

I want to make a POST call to my REST API running on another port in my pc. The http.get method works fine but when I make a POST request the server returns no response or any error in callback. Here is my code:

server.route({
method:'POST',
path:'/path1',
handler: function(req, res){
    var post_data = querystring.stringify({
        'name': 'asd',
        'price': '123'
    });
    var options = {
        host: '127.0.0.1',
        port: 2000,
        path: '/apipath',
        method:'POST',
        header:{
            'Content-Type':'application/json',
            'Content-Length': Buffer.byteLength(post_data)
        }
    };


     var post_req = http.request(options, function(reply){
            console.log('222');
            reply.setEncoding('utf8');
            reply.on('data', function (body) {
                console.log('Body: ' + body);
        });
        post_req.write(post_data);
        post_req.end();
        res(reply);
        post_req.on('error', function(e) {
        console.error(e);
        });
    });

}
});

Node.js Version : 6.9.1 LTS
Platform: Windows 10

Most helpful comment

It is hanging because you are not calling post_req.end() until the response callback is called (which it won't be called since you aren't ending the request)

Try replacing:

var post_req = http.request(options, function(reply){
  console.log('222');
  reply.setEncoding('utf8');
  reply.on('data', function (body) {
    console.log('Body: ' + body);
  });
  post_req.write(post_data);
  post_req.end();
  res(reply);
  post_req.on('error', function(e) {
    console.error(e);
  });
});

with

var post_req = http.request(options, function(reply){
  console.log('222');
  reply.setEncoding('utf8');
  reply.on('data', function (body) {
    console.log('Body: ' + body);
  });
  res(reply);
});

post_req.on('error', function(e) {
  console.error(e);
});
post_req.write(post_data);
post_req.end();

I'm going to go ahead and close since this doesn't seem to be an issue with core. Thanks!

All 3 comments

Is there a code snippet you can provide that does not use any external packages to exemplify the problem? Can you also fill in the version and platform info as well? Thanks!

Sorry for the inconvenience, I updated my post with the code snippet.

It is hanging because you are not calling post_req.end() until the response callback is called (which it won't be called since you aren't ending the request)

Try replacing:

var post_req = http.request(options, function(reply){
  console.log('222');
  reply.setEncoding('utf8');
  reply.on('data', function (body) {
    console.log('Body: ' + body);
  });
  post_req.write(post_data);
  post_req.end();
  res(reply);
  post_req.on('error', function(e) {
    console.error(e);
  });
});

with

var post_req = http.request(options, function(reply){
  console.log('222');
  reply.setEncoding('utf8');
  reply.on('data', function (body) {
    console.log('Body: ' + body);
  });
  res(reply);
});

post_req.on('error', function(e) {
  console.error(e);
});
post_req.write(post_data);
post_req.end();

I'm going to go ahead and close since this doesn't seem to be an issue with core. Thanks!

Was this page helpful?
0 / 5 - 0 ratings