How to make two JSON REST calls from Node js module in apigee?

Not applicable

I'm new to apigee and trying to achieve making two JSON REST calls from Node js module in apigee. The results of the two REST calls shall be aggregated and sent back as a response.

0 4 2,796
4 REPLIES 4

Have a look at the async module available on npm; it should get you what you're looking for.

Usage looks something like this:

var async = require('async')

async.parallel({
    callOne: function(callback) {
        request.get(URL, opts, function(e, r, body) {
            callback(e, body)
        })
    },
    callTwo: function(callback) {
        request.get(URL, opts, function(e, r, body) {
            calback(e, body)
        })
    }
}, function(err, results) {
    // now you can mash up:
    // results.callOne
    // results.callTwo
})

The below piece of code is working locally in my machine when executed with Node.js.

But when I put the same code in node.js file in Apigee, while running its throwin error "Cannot GET". Pls help.

var request = require('request');
var express = require('express');
var async = require('async');
var app = express();
var output,jsonRes1;


 async.parallel([
 function(callback) {
	request('https://api.usergrid.com', function (error, response, body) {
		if (!error && response.statusCode == 200) {
			jsonRes1 = JSON.parse(body);
		}
	});
 },	function(callback) {
	setTimeout(function() {
  request('https://api.usergrid.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var jsonRes2 = JSON.parse(body);
	 output = {'Output':{'Response1':{'Time':jsonRes1.timestamp,'Period':jsonRes1.duration}},'Response2':{'Start':jsonRes2.status.started,'CountryCode':jsonRes2.status.uptime}};
    console.dir(output);   
  }
});
	},2000);
	}
],
 function(error, results) {
  console.log(results);
});


app.listen(8001,function (req,res) {
  console.log('Server running at http://198.1.2.3:8888/');
     });



Not applicable

Hi @Ranjith Rajendran. This is a very common use case for Node .js. The following tutorial explains how to do this by leveraging request and async modules in two modes. Series and Parallel. Based on your requirements, you may take advantage of parallel processing by making these calls simultaneously and accomplish them faster. Please take a look at this sample here: https://github.com/dzuluaga/apigee-tutorials/blob/master/apiproxies/apigee-nodejs-async/README.md

The below piece of code is working locally in my machine when executed with Node.js.

But when I put the same code in node.js file in Apigee, while running its throwin error "Cannot GET". Pls help.

var request = require('request');
var express = require('express');
var async = require('async');
var app = express();
var output,jsonRes1;


 async.parallel([
 function(callback) {
	request('https://api.usergrid.com', function (error, response, body) {
		if (!error && response.statusCode == 200) {
			jsonRes1 = JSON.parse(body);
		}
	});
 },	function(callback) {
	setTimeout(function() {
  request('https://api.usergrid.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var jsonRes2 = JSON.parse(body);
	 output = {'Output':{'Response1':{'Time':jsonRes1.timestamp,'Period':jsonRes1.duration}},'Response2':{'Start':jsonRes2.status.started,'CountryCode':jsonRes2.status.uptime}};
    console.dir(output);   
  }
});
	},2000);
	}
],
 function(error, results) {
  console.log(results);
});


app.listen(8001,function (req,res) {
  console.log('Server running at http://198.1.2.3:8888/');
     });