Manipulate response from target endpoint

Hi guys,

I am new to APIGee. Anyone has an example manipulating target endpoint response in order to send back to the client a different response?

Ty, L.

0 4 149
4 REPLIES 4

Hi @leramosz - within Apigee you have a few options here and the best choice will depend on the complexity of message transformation you need to perform. See here for some details on shaping and converting messages.

Here is a nice example using the built in ExtractVariables and AssignMessage policies.

See here for an example which uses JavaScript to do a more complex JSON transformation.

Thank you. Is there a way to just extract all the response content (json) and use javascript to manipulate?

I tried to add some steps in the Target Endpoint Postflow to both extract the json response and then manipulate it in a js file.

leramosz_0-1629422920710.png

leramosz_1-1629423108346.png

leramosz_2-1629423151585.png

leramosz_3-1629423201936.png

is it possible to do something like this?

 

 

Sample reference but you can get the concept.

All we need is parse the req payload & get any specific element of interest to manipulate & traverse to replace the element and assign back the response.content

try {
var req = JSON.parse(context.getVariable("request.content"));
var id = context.getVariable("sampleId");
req.test.id = id;
context.setVariable("request.content", JSON.stringify(req));
} catch (error) {
context.setVariable("debuginfo", error);
throw error;
}

 

Sure, you can do that with a JavaScript policy alone, take a look at minimize.js in this example.