Skip to content

Instantly share code, notes, and snippets.

@jeremyroman
Created July 6, 2011 15:04
Show Gist options
  • Save jeremyroman/1067452 to your computer and use it in GitHub Desktop.
Save jeremyroman/1067452 to your computer and use it in GitHub Desktop.
var http = require('http'),
qs = require('querystring');
var body = qs.stringify({ method: 'system.connect' });
var options = {
host: 'www.goworkit.com',
port: 80,
path: '/services/json/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': body.length,
'Accept': '*/*'
}
};
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.end(body);
@jeremyroman
Copy link
Author

end takes an optional argument which is something to write before ending.
http://nodejs.org/docs/v0.4.9/api/http.html#request.end

Also, I'm writing to the request body, not the response body, because this is an HTTP client not an HTTP server.

@jeremyroman
Copy link
Author

Also, thanks for proofreading all of my gists. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment