简体   繁体   中英

How to get Request payload content at Response path class mediator using OMElement - WSO2 APIM ver 3.2.0

I am using WSO2 APIM version 3.2.0.

I have a POST request with the request payload. In the response message mediation of WSO2 APIM I have added the policy that contains the class mediator that tries to get the payload sent during the request.

OMElement element = (OMElement) mc.getEnvelope().getBody().getFirstOMChild(); log.info("payload: " + element.toString());

The above code snippet prints the response payload content but I need the request payload content at the response path.

Response message mediation with a policy added

Below is the sequence with class mediator

sequence with class mediator

Code snippet inside class mediator

OMElement element = (OMElement) mc.getEnvelope().getBody().getFirstOMChild(); log.info("payload: " + element.toString());

Pls let me know what changes to be done, to get the request payload content.

First, we have to store the request payload in a custom property in the Message Context. Then, we can use that property to retrieve the Request Payload in the Response path of the execution.

For example: You are invoking an API with JSON Payload. So, we have to first capture the sent payload and store it in a custom property in the Message Context. Given below is a sample sequence to perform the same

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--MockAPI:v1.0.0--In">
   <property name="RequestPayload" expression="json-eval($)" />
   <log level="custom">
      <property name="RequestPayload" expression="$ctx:RequestPayload" />
   </log>
</sequence>

Then, in the Response path, inside your custom class mediator, you have to access the RequestPayload property from the MessageContext to extract the stored payload. You can achieve this by using the following snippet

synapseContext.getProperty("RequestPayload");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM