Created
October 29, 2014 00:11
-
-
Save krist00fer/be38ce50d4eac3526fe1 to your computer and use it in GitHub Desktop.
Make HTTP GET Request and retrieve data as a JSON Object
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 httpGetJSON(url, callback) { | |
http.get(url, function(res) { | |
var body = ''; | |
res.on('data', function(data) { | |
console.log('httpGetJSON - data received', data); | |
body += data; | |
}); | |
res.on('end', function() { | |
console.log('httpGetJSON - all data received', body); | |
callback(null, JSON.parse(body)); | |
}); | |
}).on('error', function (err) { | |
// An error occurred while calling url | |
callback(err, null); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment