mardi 23 décembre 2014

getting JSON from an http request in NodeJS


I am having trouble returning JSON from an http GET request using NodeJS. I am fairly new to http, javascript, and NodeJS.


Reading through examples and documentation, this is what I have come up with:



var http = require('http');

var options = {
host: 'some.host.com',
path: '/some/path'
};

var callback = function(response) {
var raw = '';
response.on('data', function(d) {
raw += d;
});
response.on('end', function() {
var data = JSON.parse(raw);
});
return data;
};

http.request(options, callback).end()


I have also tried returning data from inside the response.on scope without any luck. Note that with CURL I can get data without a problem.



  1. What am I doing wrong and how can I fix the above cod to get the JSON?


Thanks for all the help!





Aucun commentaire:

Enregistrer un commentaire