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.
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:
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 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.