nivedhav
nivedhav

Reputation: 27

Modify/Remove request payload content in wso2 class mediator or xml sequence in WSO2 APIM 3.2.0

I have taken the request payload from the frontend in the wso2 apim custom sequence using the below property.

<property name="RequestPayload" expression="json-eval($)" />

Let the request payload be

{
   "token": "jhghjgjhghjg",
   "parameters": {
      "Name": "alice",
      "appName": "app",
      "Id": "Id"
   },
   "date": {
      "min": "this.startOrMin",
      "max": "this.endOrMax",
      "formats": [
         "yyyy-MM-dd HH:mm:ss",
         "yyyy-MM-dd HH:mm:ss"
      ]
   },
   "limit": "this.apiService.noOfRecords"
}

Consider I want to remove the Id field in the request payload before sending to the backend.

I have used a java class mediator in the sequence where I process this request payload taken from the property.

String ReqPayload = synapsecontext.getProperty("RequestPayload").toString();
log.info("ReqPayload: " + ReqPayload);

Suggest a way to modify/remove the payload field either in the class mediator or in the custom sequence before sending to the backend.

Upvotes: 0

Views: 759

Answers (1)

tmoasz
tmoasz

Reputation: 1330

The easiest way for your payload is to use script mediator, like below:

     <script language="js"><![CDATA[
        var message = mc.getPayloadJSON();
        delete message.parameters.Id;
        mc.setPayloadJSON(message);
     ]]></script>

It is safe, if the parameters.Id doesn't exist in payload, but be aware, thatparameters should exist. If not, you must check it before trying remove parameters.Id.

Upvotes: 1

Related Questions