1

I'm trying to perform a very simple HTTP POST with Node:

var querystring = require('querystring');
var http = require('http');

var postData = querystring.stringify({
  "source": "XXXX", 
  "target": "XXXX", 
  "create_target": true, 
  "continuous": false
});

var options = { 
  hostname: 'XXXX',
  port: 443,
  path: '/_replicate',
  method: 'POST',
  headers: { 
    'Content-Type:':'application/json',
    'Content-Length':Buffer.byteLength(postData)
  } 
};

var req = http.request(options, function(res) { 
  res.setEncoding('utf8');
  res.on('data', function (chunk) { 
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) { 
  console.log('problem with request: ' + e.stack);
});

req.write(postData);
req.end();

But the response I get is this:

problem with request: Error: socket hang up
    at createHangUpError (_http_client.js:215:15)
    at Socket.socketOnEnd (_http_client.js:293:23)
    at Socket.emit (events.js:129:20)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)

I'm using node v0.12.0.


I have seen these other similar questions on Stackoverflow, but I believe them to be different because:

Community
  • 1
  • 1
Chris Snow
  • 23,813
  • 35
  • 144
  • 309
  • Looking at the node.js code for `createHangUpError()` and when it is called, it appears to be invoked when the socket is being closed before there was any response from the other end. – jfriend00 Mar 02 '15 at 16:30

1 Answers1

1

I see that you are are making a request to a secure server. You probably should be using the request method of the https object instead.

In fact, I was able to re-create the problem using the http object and when I simply used the https object I was able to get a response without the socket closing.

HeadCode
  • 2,770
  • 1
  • 14
  • 26