-
-
Save natos/2001487 to your computer and use it in GitHub Desktop.
var request = require('request') | |
/** | |
* Handle multiple requests at once | |
* @param urls [array] | |
* @param callback [function] | |
* @requires request module for node ( https://github.com/mikeal/request ) | |
*/ | |
var __request = function (urls, callback) { | |
'use strict'; | |
var results = {}, t = urls.length, c = 0, | |
handler = function (error, response, body) { | |
var url = response.request.uri.href; | |
results[url] = { error: error, response: response, body: body }; | |
if (++c === urls.length) { callback(results); } | |
}; | |
while (t--) { request(urls[t], handler); } | |
}; |
/* | |
* How to use __request() | |
* step by step | |
*/ | |
// create an array of URLs | |
var urls = ["http://www.example.com/firts", "http://www.example.com/second", "http://www.example.com/third"]; | |
// execute the request | |
// and assign a callback | |
__request(urls, function(responses) { | |
// When all the requests are ready | |
// this callback will be called | |
// you will get an argument, is | |
// a map with all the responses | |
// of the request you made, | |
// something like this: | |
/* | |
responses = { | |
"http://www.example.com/firts": { | |
error: [object Object], | |
response: [object Object], | |
body: [object Object] | |
}, | |
"http://www.example.com/second": { | |
error: [object Object], | |
response: [object Object], | |
body: [object Object] | |
}, | |
"http://www.example.com/third": { | |
error: [object Object], | |
response: [object Object], | |
body: [object Object] | |
} | |
} | |
*/ | |
// Acces to a response: | |
// direct reference | |
var first_response = response["http://www.example.com/first"]; | |
// check for errors of the first response | |
console.log(first_response.error); | |
// access to the body of the first response | |
console.log(first_response.body); | |
// also you can reuse the reference on the original array | |
var first_response = response[urls[0]]; | |
// Iterate responses: | |
// You can simply iterate all responses | |
// to find errors or process the response | |
var url, response; | |
for (url in responses) { | |
// reference to the response object | |
response = responses[url]; | |
// find errors | |
if (response.error) { | |
console.log("Error", url, response.error); | |
return; | |
} | |
// render body | |
if (response.body) { | |
console.log("Render", url, response.body); | |
} | |
} | |
}); |
Unfortunately doesn't work. My code is this -
var links = [urls.singleMovieInfo +body.results[0].id+'?api_key='+apiKey,
urls.singleMovieInfo +body.results[0].id+'/credits?api_key='+apiKey,
urls.nowPlaying +apiKey];
request(links, function(err, responses){
if(err){
console.log(err);
} else {
console.log(responses);
It gives me the Error: options.uri is a required argument.
Cool @natos
I re-wrote a promised version, check it out here: https://gist.github.com/serganus/ebea6717ce92df88061b0b96bef7c63e
Nice work 👍
Hi,
I have multiple GET/POST API's and in that i have to set the body, header content-type and then I need to post it with http method.
Can you please share an example how this could be done.
var options = { method: 'POST',
url: 'http://sdsdksdlas.com/api',
headers:
{
'content-type': 'application/json' },
body:
{ header: 'networork=verizon,asnum=1sfa000+11dasd6,location_id=0',
ip: 'NULL' },
json: true };
$http(options, function (error, response, body) {
console.log('Response code: ' + response.statusCode);
if (!error && response.statusCode == 200) {
console.log('GET authentication was successful');
console.log(body);
}
else
{
console.log('GET failed');
throw new Error(response.statusCode + ' response code received');
}
});
how if the process is post and contain json file?
how if the process is post and contain json file?
Change:
while (t--) { request(urls[t], handler); }
to
while (t--) { request(urls[t], { json: true }, handler); }
and:
if (response.body) {
console.log("Render", url, response.body);
}
to:
if (response.body) {
Object.keys(response.body).forEach(e => result=`${response.body[e]}`);
console.log(result);
}
When I use request 2.66, it can't find response.request