Created
June 26, 2011 11:57
-
-
Save kixxauth/1047559 to your computer and use it in GitHub Desktop.
Make a request to an http server
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
// Call this script with a http URL as the single argument: | |
// node test_request.js http://www.google.com | |
var http = require('http'); | |
var url = require('url'); | |
parsed = url.parse(process.argv[2]); | |
var opts = { | |
port: 80, | |
host: parsed.hostname, | |
path: parsed.pathname, | |
method: 'GET' | |
}; | |
var req = http.request(opts, function (res) { | |
console.log(); | |
console.log('got callback...'); | |
var buff = ''; | |
res.setEncoding('utf8'); | |
res.on('end', function () { | |
console.log(); | |
console.log('body length:', buff.length); | |
console.log(); | |
console.log('END'); | |
console.log(); | |
}); | |
res.on('data', function (chunk) { | |
buff += chunk; | |
}); | |
console.log(); | |
console.log(res.headers); | |
}); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment