Transforming Content Types on Response Flow — WSO2 Micro Integrator

Nadee Poornima
2 min readOct 15, 2023

Hello, my dears …

Ref: https://tenor.com/en-GB/view/hi-oh-wait-ron-weasley-harry-potter-gif-4987840

Hope you are doing great. :-) After a time, I decided to write a short blog describing how to change the content type or message type of the response message.

Let’s assume your backend is sending a JSON-type response. However, it needs to be sent to the client as an XML-type message, this can be achieved through WSO2 Micro Integrator with a simple property.

In that case, you need to set the “messageType” property within the response flow as follows.

<property name="messageType" scope="axis2" type="STRING" value="application/xml"/>

According to the example, we have set the message type as “application/xml”. Therefore, when sending the back-end response through the response flow, the “Content-Type” header will be set as the “application/xml”; thus, the response message will be built and formatted with that relevant formatter and builder. Therefore, the response message will be sent as an XML message to the client. Let’s see an example.

Ref: https://tenor.com/en-GB/view/harry-potter-daniel-radcliffe-wand-cute-testing-my-magic-powers-gif-17537623
  • Example: With this property, the following message,
{
"hello": "world"
}

can be converted into the following XML format.

<jsonObject>
<hello>world</hello>
</jsonObject>
Ref: https://www.wattpad.com/605693745-harry-potter-gifs-hermione-happy

Here, you can specify any content type as your requirement to build the response message, such as “text/plain” or “application/octet-stream”.

<property name="messageType" scope="axis2" type="STRING" value="Your content type"/>

However, you need to make sure to enable the relevant formatter and builder on the WSO2 Micro Integrator by adding the configuration to the “deployment. toml” file. Otherwise, you will observe builder errors.

  • Sample API code to test for you:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/test" name="ContentTypeChange" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET">
<inSequence>
<log level="custom"/>
<send>
<endpoint key="TetsEndpoint"/>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>

So … done … give a try and let me know whether this is useful.

Bye-bye…. see you soon. :-) Stay safe and happy ❤ ❤ ❤

Ref: https://tenor.com/en-GB/view/hi-oh-wait-ron-weasley-harry-potter-gif-4987840

Reference

--

--