Send response to client from Array with multiple payloads

We have a requirement where API Proxy will receive an array in request content. Array will have a set of JSON's, each JSON will have a co-relationId, Operation Name and Payload.

My requirement is to call an Backend Server based on the Operation Name(Host will be same but Path changes for each operation). For example I will receive the below payload from Client.

 

[
	{
		CorelationID: "1234567",
		OperationName: "read",
            Payload: {}
	},
	{
		CorelationID: "345678",
		OperationName: "write",
            Payload: {}
	},
	{
		CorelationID: "6789964",
		OperationName: "execute",
            Payload: {}
	}
]

 

I need to call target server multiple times based on the OperationName(Each operation will have different URI/Paths) and need to send corelationID and payload as request body. I used Java Script with for loop to extract and print all the values from JSON(CorelationID, OperationName and Payload). I need to call the target server in the same for loop to send the req content. Am looking for sample code to do this using JavaScript(httpClient).
Could someone please suggest if there is any other feasible solution to achieve this.

1 1 309
1 REPLY 1

Please see previous Q&A here. The final answer there links to this code , which shows you how to  send out multiple calls from a JavaScript callout, even limiting the number of concurrent outbound calls. You would have to modify it:

  •  use POST instead of GET, and to pass the payload. 
  • Your callback would have to accumulate the responses from the multiple outbound calls.
  • When you get the final response in JS, the JS will complete and then your API proxy can relay the aggregated response to the original client.

BTW, This is better done in an Application Integration flow.  What you described is called the "scatter-gather pattern".  App Integration will give you a better visual for what's happening and will be easier to maintain longer term, in my opinion.