Created
April 3, 2011 06:49
-
-
Save mattijs/900243 to your computer and use it in GitHub Desktop.
Send request and print response using node.js
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 util = require('util'); | |
var fs = require('fs'); | |
var http = require('http'); | |
// Check for an input file | |
if (2 >= process.argv.length) { | |
console.error('An input file must be specified'); | |
process.exit(1); | |
} | |
// Pop the file of the script arguments (assume its the last argument) | |
var file = process.argv.pop(); | |
// Try to read the input file | |
try { | |
var content = fs.readFileSync(file, 'utf-8'); | |
} catch (error) { | |
console.error('Could not read file "' + file + '". Error: ' + error); | |
process.exit(1); | |
} | |
// Split header and body | |
var parts = content.split(/\r?\n\r?\n/, 2); | |
var headerPart = parts.shift().trim(); | |
var bodyPart = parts.length > 0 ? parts.shift().trim() : ''; | |
// Process the headers | |
var headerLines = headerPart.split(/\r?\n/); | |
var requestLine = headerLines.shift(); | |
var hostLine = ''; | |
var headers = {}; | |
for (var i = 0; i < headerLines.length; i++) { | |
var line = headerLines[i]; | |
var headerParts = line.split(':', 2); | |
var headerKey = headerParts.shift().trim(); | |
var headerValue = headerParts.shift().trim(); | |
// Special case for Host header, we need this seperately for node's | |
// request method. This could be circumvent by using the ClientRequest object directly | |
if ('host' === headerKey.toLowerCase()) { | |
hostLine = headerValue; | |
continue; | |
} | |
// Save the header | |
headers[headerKey] = headerValue; | |
} | |
// Build the request options | |
var options = { | |
headers: headers | |
}; | |
// Process the request line to retreive the method and path | |
var requestLineParts = requestLine.split(' '); | |
options.method = requestLineParts.shift().trim().toUpperCase(); | |
options.path = requestLineParts.shift().trim(); | |
// Process the host and port from the hostLine | |
var hostParts = hostLine.split(':', 2); | |
options.host = hostParts.shift().trim(); | |
options.port = hostParts.length > 0 ? hostParts.shift().trim() : 80; | |
// Make the actual request | |
var request = http.request(options, function(response) { | |
var body = ''; | |
// Write the status line | |
util.puts('HTTP/' + response.httpVersion + ' ' + response.statusCode + ' ' + http.STATUS_CODES[response.statusCode]); | |
// Make sure we can handle chunked transfer | |
response.on('data', function(chunk) { | |
body += chunk; | |
}); | |
// Once done present the result | |
response.on('end', function() { | |
// Print the response headers | |
for (header in response.headers) { | |
var value = response.headers[header]; | |
util.puts(header + ': ' + value); | |
} | |
util.puts(''); | |
// Print the body | |
util.puts(body); | |
}); | |
}); | |
// Handle errors | |
request.on('error', function(error) { | |
console.error('Error making request: ' + error); | |
process.exit(2); | |
}); | |
// Send the request with body | |
request.write(bodyPart); | |
request.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment