Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created May 24, 2010 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrchrisadams/411589 to your computer and use it in GitHub Desktop.
Save mrchrisadams/411589 to your computer and use it in GitHub Desktop.
// import needed libraries
var sys = require('sys'),
http = require('http');
// define what the connection connection to website will be:
var world_bank = http.createClient(80, 'http://open.worldbank.org');
// define where we're connecting on on the site
var request = world_bank.request('GET', '/countries/all/indicators/EN.ATM.CO2E.PC?date=2002:2002&format=json',
{'host': 'http://open.worldbank.org'});
// define what to do when response comes back
request.addListener('response', function (response) {
// snippet of debugging code so we can see if the request code is '200', meaning 'good', and a bit of extra data
sys.puts('STATUS: ' + response.statusCode);
sys.puts('HEADERS: ' + JSON.stringify(response.headers));
// set encoding for showing content
response.setEncoding('utf8');
// bit by bit build the page - when data is received, simply 'puts' to the output of the terminal
response.addListener('data', function (chunk) {
sys.puts('BODY: ' + chunk);
});
});
// with all that setup, start the request, according to the API documentation
// but I'm getting complaints that the httpClient has non end() method to call here...
request.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment