Chris Snow
Chris Snow

Reputation: 24616

Error: socket hang up using node v0.12.0

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:

Upvotes: 1

Views: 5219

Answers (1)

HeadCode
HeadCode

Reputation: 2828

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.

Upvotes: 1

Related Questions