I want to club multiple responses into a single response

In the request side i am calling multiple backend endpoints (6 endpoints) by using service callout and stored these responses in different variables and finally calling backend endpoint. 

In the response side, I am retrieving those request variables in response JavaScript in order to club all the responses into a single response including original backend response i.e. concatenating 7 responses. 

Below is one such Service callout. 

jyosthnasai_0-1708492423100.png

Here is the Javascript code which was implemented: 

 var FirstResponse = JSON.parse(context.getVariable("FirstResponse.content"));
var SecondResponse = JSON.parse(context.getVariable("SecondResponse.content"));
var ThirdResponse = JSON.parse(context.getVariable("ThirdResponse.content"));
var FourthResponse = JSON.parse(context.getVariable("FourthResponse.content"));
var FifthResponse = JSON.parse(context.getVariable("FifthResponse.content"));
var SixthResponse = JSON.parse(context.getVariable("SixthResponse.content"));
var res = JSON.parse(context.getVariable("response.content"));

var finalResponse = (FirstResponse, SecondResponse, ThirdResponse, FourthResponse, FifthResponse, SixthResponse, res);

context.setVariable("FinalResponse", JSON.stringify(finalResponse));

Issue is, whatever the response we're getting from original backend, the same response is going to frontend without concatenation.  I am doing anything wrong?

Sample Response from original backend endpoint:  

{
    "@odata.context": "https://google.com/users",
    "value": [
        {
            "businessPhones": [],
            "displayName": "Sandaga, Salvador",
            "givenName": "Salvador",
            "jobTitle": null,
            "mail": "(PII Removed by Staff)",
            "mobilePhone": "(PII Removed by Staff)",
            "officeLocation": null,
            "preferredLanguage": null,
            "surname": "Sandaga",
            "userPrincipalName": "(PII Removed by Staff)",
            "id": "36350a42-4cd9-42a0-b955-e28aafbf1c26"
        }
}
 
Sample response from service call out:
{
"@odata.context": "https://google.com/users",
"value": [
{
"businessPhones": [],
"displayName": "1000324564_MAMDOUH_ALI_M_DAN",
"givenName": null,
"jobTitle": null,
"mail": null,
"mobilePhone": "(PII Removed by Staff)",
"officeLocation": null,
"preferredLanguage": null,
"surname": null
}
]
}
5 4 145
4 REPLIES 4

Hi @jyosthnasai . You are not overwriting the response.content variable. By default, the response returned from the target will be returned to the client. If you want to send custom/modified response to the client, you need to map that to the response.content variable. So, in your JS script, you can map the finalResponse variable to response.content as follows:


@jyosthnasai wrote:

context.setVariable("FinalResponse", JSON.stringify(finalResponse));


context.setVariable("response.content", JSON.stringify(finalResponse));

Another way could be using the AssignMessage policy after the JavaScript policy in case you have to use the FinalResponse variable in the API flow too:

muq_ash_0-1708511393457.png

 

@muq_ash Thanks for the response..!

I have tried both ways but still it is same issue. 


@jyosthnasai wrote:

var finalResponse = (FirstResponse, SecondResponse, ThirdResponse, FourthResponse, FifthResponse, SixthResponse, res);


This is not valid JavaScript, as far as I am aware.  I suspect your JS would fail at this point.  

And also, the point that @muq_ash made is also important. You would need to correct both issues.

 

var finalResponse = Object.assign({}FirstResponse, SecondResponse, ThirdResponse, FourthResponse, FifthResponse, SixthResponse, res);

context.setVariable("FinalResponse", JSON.stringify(finalResponse));

I have tried with above but didn't get response as expected. @dchiesa1 @muq_ash can you help me on this how can i club all responses at a time in JS while sending back to consumer. 

Tried as suggested by @muq_ash but no use. 

jyosthnasai_0-1708879681188.png