Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Apigee Edge: Unable to set response.content value in Postclient flow.

I am trying to set response.content value from null or empty to "" using JS in post client flow before logging in the splunk. 

Not able to replace and set the value of response.content. 

Refer the below code snippet. Additionally, in the trace I see Not="" in response.content. 

JS Code:

Refer else Part.

 

function isValidJSON(inputContent){
    try{
        JSON.parse(inputContent);
        return true;
    }
    catch(error){
        return false;
    }
}
var responseContent = context.getVariable("response.content");
if (responseContent){
if(!isValidJSON(responseContent)){
responseContent = "'"+responseContent+"'";
context.setVariable("response.content", JSON.stringify(responseContent));
}
}
else{
context.setVariable("response.content", JSON.stringify(''));
}

 

Trace Screenshot:

ashutoshcupsima_1-1724161561540.png

0 4 274
4 REPLIES 4

Hello,

It's better to do it before postclientflow (like in PostFlow->Response phase), simply because postclientflow designed to perform only logging-related functionality.

Having code execute after the client receives your proxy's response with a PostClientFlow 


A PostClientFlow can include the following policies only. No other policies can be used in a PostClientFlow:


 

Hi @nmarkevich thanks for assiting @ashutoshcupsima and for sharing your expertise 

Thank you @nmarkevich for your response. 
But how can we implement masking of sensitive values before storing them in Splunk, using JavaScript to mask the response.content. 

Hi @ashutoshcupsima,

Masking sensitive data before logging it is actually a good security practice!

A JavaScript policy in Response phase can do it.

var responseContent = JSON.parse(context.getVariable("response.content"));
if (responseContent.sensitiveData) {
    responseContent.sensitiveData = "***";
}
context.setVariable("flow.log", JSON.stringify(responseContent));

You can also use more sophisticated approaches, like masking only part of the data.