Thursday, January 22, 2015

How to send string content as the response from WSO2 ESB Proxy/API

We can send XML content or JSON content as the response out from our WSO2 ESB proxy/REST API. But there may be a scenario where we want to send string content (which is not in XML format) as the response of our service. Following synapse code snippet shows you how to do it

As an example think you have to send following string content to your client service

<result1>malintha</result1>+<result2>adikari</result2>

Note :
  • Above is not in XML format. So we cannot generate this directly through payload factory mediator.
  • We have to send <,> symbols inside the response, but WSO2 ESB doesn't allow to keep those inside your synapse configuration.
1. First you have to encode above expected response. You can use this tool to encode your xml. We get following xml after encoding in our example

&lt;result1&gt;malintha&lt;/result1&gt;+&lt;result2&gt;adikari&lt;/result2&gt;

Note : If you want to encode dynamic payload content you can use script mediator or class mediator for that task

2. Now we can attach the require string content to our payload as follows

 <outSequence>  
       <payloadFactory media-type="xml">  
         <format>  
          <ms11:text xmlns:ms11="http://ws.apache.org/commons/ns/payload">$1</ms11:text>  
         </format>  
         <args>  
          <arg value="&lt;result1&gt;malintha&lt;/result1&gt;+&lt;result2&gt;adikari&lt;/result2&gt;"/>  
         </args>  
       </payloadFactory>  
       <property name="messageType" value="text/plain" scope="axis2"/>  
       <send/>  
    </outSequence>  

Here we are using payload factory mediator to create our payload. You can see still our media-type is XML. Then load our string content as argument value and finally we change the message type to "text/plain". So this would return string content as it's response.

No comments:

Post a Comment