Created
January 9, 2014 21:49
-
-
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".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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