How to get data from Node.js http get request

I am trying to return my function for an http get request, however, whatever I do, it seems to be lost in scope? I am leaving a newbie to Node.js, so any help would be appreciated

function getData(){ var http = require('http'); var str = ''; var options = { host: 'www.random.org', path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' }; callback = function(response) { response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { console.log(str); }); //return str; } var req = http.request(options, callback).end(); // These just return undefined and empty console.log(req.data); console.log(str); } 
+69
javascript get request
Oct 23 '13 at
source share
7 answers

Of course, your logs return undefined : you are logged before the request is completed. The problem is not in the area, but in asynchrony.

http.request is asynchronous, so it takes a callback parameter as a parameter. Do what you have to do in the callback (the one you pass to response.end ):

 callback = function(response) { response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { console.log(req.data); console.log(str); // your code here if you want to use the results ! }); } var req = http.request(options, callback).end(); 
+105
Oct 23 '13 at 10:45
source

A shorter example using http.get:

 require('http').get('http://httpbin.org/ip', (res) => { res.setEncoding('utf8'); res.on('data', function (body) { console.log(body); }); }); 
+10
Oct. 15 '16 at 7:00
source

from learnyounode:

 var http = require('http') http.get(options, function (response) { response.setEncoding('utf8') response.on('data', console.log) response.on('error', console.error) }) 

'options' is the host / path variable

+6
Dec 16 '15 at 20:41
source

From LearnYounode:

 var http = require('http') var bl = require('bl') http.get(process.argv[2], function (response) { response.pipe(bl(function (err, data) { if (err) return console.error(err) data = data.toString() console.log(data) })) }) 
+6
Mar 28 '16 at 5:45
source

This is my solution, although for sure you can use many modules that give you an object like a promise or the like. Anyway, you were missing another callback

 function getData(callbackData){ var http = require('http'); var str = ''; var options = { host: 'www.random.org', path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' }; callback = function(response) { response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { console.log(str); callbackData(str); }); //return str; } var req = http.request(options, callback).end(); // These just return undefined and empty console.log(req.data); console.log(str); } 

somewhere else

 getData(function(data){ // YOUR CODE HERE!!! }) 
+1
Mar 16 '16 at 18:56
source

A simple working example of an Http request using a node.

 const http = require('https') httprequest().then((data) => { const response = { statusCode: 200, body: JSON.stringify(data), }; return response; }); function httprequest() { return new Promise((resolve, reject) => { const options = { host: 'jsonplaceholder.typicode.com', path: '/todos', port: 443, method: 'GET' }; const req = http.request(options, (res) => { if (res.statusCode < 200 || res.statusCode >= 300) { return reject(new Error('statusCode=' + res.statusCode)); } var body = []; res.on('data', function(chunk) { body.push(chunk); }); res.on('end', function() { try { body = JSON.parse(Buffer.concat(body).toString()); } catch(e) { reject(e); } resolve(body); }); }); req.on('error', (e) => { reject(e.message); }); // send the request req.end(); }); } 
0
Jun 12 '19 at 15:52
source

I think it's already too late to answer this question, but recently I ran into the same problem, my use case was to call the JSON API with page numbering, get all the data from each page numbering and add them into one array.

 const https = require('https'); const apiUrl = "https://example.com/api/movies/search/?Title="; let finaldata = []; let someCallBack = function(data){ finaldata.push(...data); console.log(finaldata); }; const getData = function (substr, pageNo=1, someCallBack) { let actualUrl = apiUrl + '${substr}&page=${pageNo}'; let mydata = [] https.get(actualUrl, (resp) => { let data = ''; resp.on('data', (chunk) => { data += chunk; }); resp.on('end', async () => { if (JSON.parse(data).total_pages!==null){ pageNo+=1; somCallBack(JSON.parse(data).data); await getData(substr, pageNo, someCallBack); } }); }).on("error", (err) => { console.log("Error: " + err.message); }); } getData("spiderman", pageNo=1, someCallBack); 

As @ackuser mentioned, we can use another module, but in my case I had to use the https node. Hope this helps others.

0
Jun 14 '19 at 9:33
source



All Articles