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:
- Node.js POST causes [Error: socket hang up] code: 'ECONNRESET' - is an issue POSTing non-ascii characters.
- nodejs https Error: socket hang up with localhost - is an issue POSTing to localhost
- NodeJS HTTP request POST ERROR socket hang up - seems to be doing a PUT rather than a POST
- socket hang up node 0.8.17 - is not calling req.end()
- Error: socket hang up - is not stringifying their POST data.
- node.js socket hang up over https - seems to be a version of Node older than v0.12.0 (based on the question date)
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.