Skip to content

Instantly share code, notes, and snippets.

@julesbravo
Created January 9, 2014 21:49
Show Gist options
  • Save julesbravo/8342688 to your computer and use it in GitHub Desktop.
Save julesbravo/8342688 to your computer and use it in GitHub Desktop.
Test for including body in HTTP GET requests using http.request. Results in empty body and "problem with request: socket hang up".
var http = require('http');
var options = {
hostname: 'localhost',
port: 23000,
path: '/',
method: 'GET'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
var http = require('http');
function handleRequest(req, res) {
var body = "";
req.on('data', function(chunk) {
body += chunk.toString();
});
req.on('end', function() {
console.log(body);
});
}
var serverHTTP = http.createServer(handleRequest).listen(23000, function() {
console.log("Listening on port 23000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment