Sunday, August 9, 2015

How to do a URL encode in WSO2 ESB

I have recently came across a requirement to send an XML paylod in URL encoded format to the backend. I was able to get it done with WSO2 ESB script mediator

Request to ESB is as follows:
<request><user>Sajith</user></request>


Backend expects the request as follows
http://localhost:8001/myservice?RequestXML=%3CRequest%3E%3CUser%3ESajith%3C%2FUser%3E%3C%2FRequest%3E


This was achieved by the following code. (Using javascript method encodeURL)
     <script language="js" description="">
         mc.setProperty("uri.var.encodedBody", encodeURI(mc.getPayloadXML()));
     </script>

     <send>
         <endpoint>
             <http method="post" uri-template="http://localhost:8001/myservice?RequestXML={uri.var.encodedBody}"/>
         </endpoint>
     </send>

No comments: