An API hosted in the WSO2 API Cloud receives requests sent from your consumer applications to your backend, and then sends the responses from the backend to you. The default flow of events of an API hosted in the WSO2 API Cloud is shown in the diagram below.
If you need to modify or transform the requests and responses of your API, you need to engage a message mediation that intercepts the default flow. The API Gateway has a default mediation flow, which you can extend using mediation policies. You can create a custom mediation policy manually, or using a tool, and then engage it with the API. This will enable you to modify the default mediation flow for different use cases according to your requirement.
Let's look at a scenario, where custom mediation policies can be applied.
In this example, your consumer application sends and receives messages in JSON format and the backend service uses XML. Therefore, your consumer application sends the request payload using JSON. However, your backend service expects the request payload to be in XML format. Similarly, your consumer application expects a JSON response from your API, but the backend service is only capable of sending the responses in XML format. You have to convert the JSON messages sent from the application to the backend service into an XML and also convert the XML messages sent back from the backend service as the response to the consumer application into JSON format. WSO2 API Cloud allows you to add mediation policies that convert the messages to the correct format, as shown in the diagram below.
You can add a mediation sequence to the In Flow, which converts the JSON request from the application and sends it in XML format to the backend service. You will also need to convert the XML responses from the backend service, to JSON which can be detected by your application. To convert the responses from XML to JSON, you can add another mediation sequence to the Out Flow.
WSO2 API Cloud offers a wide variety of custom sequences out-of-the-box, which can be applied according to your requirements. Following are some of the custom message mediation examples:
- Changing the content type
- Extracting, transforming or replacing the contents of a message
- Adding headers
- Filtering messages
- Routing to different backends
- Working with properties
- Reading path and query parameters inside the sequence
- Moving query parameters to the REST path
- Disable chunking
- Debugging your requests
- Service Chaining with API Cloud
- Other custom mediation scenarios
- Viewing sequences uploaded to APIs
If you are using a respond mediator in a custom sequence and you want to enable Analytics to publish the statistics, you need to include the
APIMgtResponseHandler class before the respond mediator in a manner similar to the following:
When you include the
APIMgtResponseHandler class, make sure that you add the
ENDPOINT_ADDRESS property to populate the destination address for statistics (API Usage by Destination).