How to loop the service callout for set of array request?

Hello Team,

Iam getting request from backend having JSON Array values as below format

Req:

{"X": [ {"A":"256","B":"100","C": "Z"}, {"A":"236","B":"100","C": "Z"}]}

I need to call backend for every "A" value by using service callout policy. Is it possible to loop the service call for 'A' value in the array?

Thank you.

0 2 596
2 REPLIES 2

You cannot do that. The proxy flow logic in Apigee is governed by the Domain Specific Language (DSL) for apigee configuration, and that DSL does not include logic for loops. It does include conditions, but not loops. Therefore it is not possible for you to invoke ServiceCallout, or any Apigee policy, in a loop.

If you want to loop, you can use JavaScript. And from within JavaScript you can invoke external systems with the httpClient. You should use the onComplete callback mechanism. The correct use of the onComplete callback is described here.

There are limits that apply to the JavaScript policy:

  • a timelimit for execution of the policy. You can raise this with the timeLimit attribute.
  • a limit to the number of pending callbacks for the httpClient. If you have more than 10 A's in your array, you may run into this. Here is a recent Q&A that discusses the issue.

Not applicable

Doing loop is anti-pattern. You can do calls within javascript code.

But I wouldn't suggest to do.