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:

1
  • 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. Commented Mar 2, 2015 at 16:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.