WSO2 ESB append full body to a POST parameter

When you have to invoke a back end service which can take only on parameter and you need to send your full soap envelop body as single post parameter you can use script mediator and payload factory mediator as following. I have an api which calls to "http://localhost:9000/services/SimpleStockQuoteService"  and response is received by the "second_sequence".
In the second_sequence response is processed and assign it single parameter using script mediator and payload factory.

API


<api xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteAPI2" context="/stockquote_api">
   <resource methods="GET" uri-template="/view/{symbol}">
      <inSequence>
         <payloadFactory>
            <format>
               <m0:getQuote xmlns:m0="http://services.samples">
                  <m0:request>
                     <m0:symbol>$1</m0:symbol>
                  </m0:request>
               </m0:getQuote>
            </format>
            <args>
               <arg expression="get-property('uri.var.symbol')"/>
            </args>
         </payloadFactory>
         <send receive="second_sequence">
            <endpoint>
               <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </resource>
</api>
                     

SEQUENCE



<sequence xmlns="http://ws.apache.org/ns/synapse" name="second_sequence">
   <property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"/>
   <script language="js">
      <![CDATA[
                     var payload = mc.getEnvelope().getBody().getFirstElement().toString();
                     payload = encodeURI(payload);        
                     mc.setProperty("test",payload);
        ]]>
    </script>
      <payloadFactory>
         <format>
            <getSimpleQuote>
               <symbol>$1</symbol>
            </getSimpleQuote>
         </format>
         <args>
            <arg xmlns:ns="http://org.apache.synapse/xsd" expression="$ctx:test"/>
         </args>
      </payloadFactory>
      <log level="custom">
         <property xmlns:ns="http://org.apache.synapse/xsd" name="xxxxxxx....................." expression="$ctx:test"/>
      </log>
      <log level="full"/>
      <send>
         <endpoint>
            <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="get"/>
         </endpoint>
      </send>
      <log level="full" category="DEBUG"/>
   </sequence>


You will find that in the sequence it sends the request as follows

GET /services/SimpleStockQuoteService/getSimpleQuote?symbol=%253cns:getQuoteResponse%2520xmlns:ns=%2522http://services.samples%2522%253e%253cns:return%2520xmlns:ax21=%2522http://services.samples/xsd%2522%2520xmlns:xsi=%2522http://www.w3.org/2001/XMLSchema-instance%2522%2520xsi:type=%2522ax21:GetQuoteResponse%2522%253e%253cax21:change%253e-2.878332384294565%253c/ax21:change%253e%253cax21:earnings%253e13.343243692880526%253c/ax21:earnings%253e%253cax21:high%253e83.90879243287203%253c/ax21:high%253e%253cax21:last%253e80.71927384911324%253c/ax21:last%253e%253cax21:lastTradeTimestamp%253eMon%2520Nov%252025%252012:10:46%2520PST%25202013%253c/ax21:lastTradeTimestamp%253e%253cax21:low%253e84.13935291865967%253c/ax21:low%253e%253cax21:marketCap%253e4.856632338341972E7%253c/ax21:marketCap%253e%253cax21:name%253eibm%2520Company%253c/ax21:name%253e%253cax21:open%253e84.41127937872128%253c/ax21:open%253e%253cax21:peRatio%253e-17.33795236794525%253c/ax21:peRatio%253e%253cax21:percentageChange%253e3.7341845696623412%253c/ax21:percentageChange%253e%253cax21:prevClose%253e-77.08061373503116%253c/ax21:prevClose%253e%253cax21:symbol%253eibm%253c/ax21:symbol%253e%253cax21:volume%253e9235%253c/ax21:volume%253e%253c/ns:return%253e%253c/ns:getQuoteResponse%253e






Comments

  1. Hi, thank you for write this helpful post. Now i have a question. Do you have another function that encode the value from < to < instead to %3c. Thanks in advance and regards.

    ReplyDelete

Post a Comment

Popular posts from this blog

WSO2 ESB Removing full soap header using enrich mediator.

Getting started with WSO2 Device Cloud APIs

Running a Standalone WSO2 IoT Server.