How to implement concurrent/parallel calls to multiple rest APIs(backend) using JavaScript and merge the response from the all the backend APIs?

amar333n
Participant III

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 Solved
1 6 38.8K
2 ACCEPTED SOLUTIONS

Not applicable

You can use the "async" Node.js module - the "parallel" function:

https://caolan.github.io/async/docs.html#.parallel

View solution in original post

@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');
});


asyncdemo-rev2-2016-07-24.zip

Hope it helps, keep us posted if you have any queries.

View solution in original post

6 REPLIES 6

Not applicable

You can use the "async" Node.js module - the "parallel" function:

https://caolan.github.io/async/docs.html#.parallel

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');
});


asyncdemo-rev2-2016-07-24.zip

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.