I have 4 rest APIs(Backend). I want to call all the 4 rest APIs at a time and combine the response of all the 4 APIs only if I receive the success response from all the 4 APIs. How to implement this?
Solved! Go to Solution.
You can use the "async" Node.js module - the "parallel" function:
@Amar N ,
As said by @Ivan Novakov , You need to use Apigee Node.JS target & async, request modules. It should be pretty straightforward. Please find below server.js code & Apigee Proxy bundle which demonstrates same capability.
var http = require('http'); var async = require('async'); var request = require('request'); console.log('node.js application starting...'); var svr = http.createServer(function(req, resp) { // an example using an object instead of an array async.parallel({ one: function(callback) { console.log("One"); request('http://mocktarget.apigee.net/json', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); }, two: function(callback) { console.log("Two"); request('http://mocktarget.apigee.net/', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); }, three: function(callback) { console.log("Three"); request('https://httpbin.org/ip', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); }, four: function(callback) { console.log("Four"); request('https://httpbin.org/headers', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); } }, function(err, results) { // results is now equals to: {one: 1, two: 2} resp.writeHead(200, {"Content-Type": "application/json"}); console.log(results); resp.end(JSON.stringify(results)); }); }); svr.listen(9000, function() { console.log('Node HTTP server is listening'); });
Hope it helps, keep us posted if you have any queries.
You can use the "async" Node.js module - the "parallel" function:
kindly, If I use this solution it will return the response as a single object ?
@Amar N ,
As said by @Ivan Novakov , You need to use Apigee Node.JS target & async, request modules. It should be pretty straightforward. Please find below server.js code & Apigee Proxy bundle which demonstrates same capability.
var http = require('http'); var async = require('async'); var request = require('request'); console.log('node.js application starting...'); var svr = http.createServer(function(req, resp) { // an example using an object instead of an array async.parallel({ one: function(callback) { console.log("One"); request('http://mocktarget.apigee.net/json', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); }, two: function(callback) { console.log("Two"); request('http://mocktarget.apigee.net/', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); }, three: function(callback) { console.log("Three"); request('https://httpbin.org/ip', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); }, four: function(callback) { console.log("Four"); request('https://httpbin.org/headers', function (error, response, body) { if (!error && response.statusCode == 200) { callback(null, body); } else { callback(true, {}); } }); } }, function(err, results) { // results is now equals to: {one: 1, two: 2} resp.writeHead(200, {"Content-Type": "application/json"}); console.log(results); resp.end(JSON.stringify(results)); }); }); svr.listen(9000, function() { console.log('Node HTTP server is listening'); });
Hope it helps, keep us posted if you have any queries.
@Anil Sagar @ Google , In the above example you are setting the headers value has resp.writeHead(200,{"Content-Type":"application/json"});
What if I want to set the headers value that is coming from response(server side).
I tried the below code which is not setting :
function (statusCode, results, response) { resp.writeHead(statusCode, response.headers);
resp.end(results);
});
Can you help me with this?
HI Sagar, do we have Node.JS target & async, request modules in Apigee cloud version ?
No; since Anil wrote this reply, the Node.js target has been deprecated.
Ask a new question and We'll try to answer it.
User | Count |
---|---|
3 | |
2 | |
1 | |
1 | |
1 |