Calling api-proxy using Java callout

Hi,

I want to call one API proxy in apigee from another api-proxy in apigee, using Java Callout. Could you please help with an example.

I haven't been successful in finding examples stating a callout to an api-proxy from java-callout policy.

0 3 193
3 REPLIES 3

Your title says "Java callout" but then in the body of your question, you said "JS callout" and also "java callout". I'm not clear.

If you want to call somethiing in a loop you can do that from within a JS callout. There is an httpClient in the JS callout. An example for how to use it is here.

You can send requests in a loop. Here is a related recent question on the topic.

Here;s some example code that invokes things in a loop, and then combines the results.

var baseUrl = 'https://example.com/billingInfo';
var ids = [182872, 19882, 2133];
var accumulator = {};


function checkDone() {
  if (Object.keys(accumulator).length == ids.length) {
    context.setVariable('combined', JSON.stringify(accumulator, null, 2));
  }
}


// the asynchronous response handler
function onComplete(id) {
  return function(response, error) {
    if (response) {
      if (response.status == 200) {
        accumulator[id] = myTransform(response.content);
      }
      else {
        accumulator[id] = "non-success response";
      }
    }
    else {
      accumulator[id] = 'Error: ' + error;
    }
    checkDone();
  };
}


// send out one request
function invokeOne(id,ix) {
  var url = baseUrl + '?id=' + id ;
  var headers = {
        Authorization : 'optional, pass whatever you like',
        'ID-Index' : ix
      };
  var req = new Request(url, 'GET', headers);
  httpClient.send(req, onComplete(id));
}


// set the default value (empty object)
context.setVariable('combined', JSON.stringify(accumulator, null, 2));


// action starts here
ids.forEach(invokeOne);


Thanks for your answer. I have updated my post and I did actually mean Java Callout.

ok thanks for that. Can you tell me: Why Java callout? If you just want to invoke an HTTP endpoint, why do you need to do it from within a Java callout?