Wednesday, March 12, 2014

How to conver http POST request to http GET request using WSO2 ESB proxy service


In this blog, I am going to discuss how to send a HTTP GET request using WSO2 ESB proxy service. Here is the sample scenario.





Client sends a post request with a SOAP message to the proxy service and proxy service retrieve a parameter form the payload and make a HTTP get request with a URI parameter to get the response form the actual backend. Back end service is a rest service. Then Proxy service get the rest response and convert it in to SOAP message and send back to the client.

Below proxy configuration make above scenario worked.

Prerequisite
WSO2 ESB 4.7.0
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="stockQuote"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="symbol1" value="2" scope="default" type="STRING"/>
         <property xmlns:m1="http://services.samples/xsd"
                   xmlns:m0="http://services.samples"
                   name="symbol"
                   expression="//m0:getQuote/m0:request/m1:symbol"
                   scope="default"
                   type="STRING"/>
         <property xmlns:ns="http://org.apache.synapse/xsd"
                   name="REST_URL_POSTFIX"
                   expression="fn:concat('?symbol=',get-property('symbol1'))"
                   scope="axis2"
                   type="STRING"/>
         <log level="custom">
            <property name="url" expression="get-property('axis2','REST_URL_POSTFIX')"/>
         </log>
         <property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
         <send>
            <endpoint>
               <address uri="http://localhost:8080/stockquote/getQuote" format="rest"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <property name="messageType" value="text/xml" scope="axis2"/>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>
                                

Once you send a request with below payload to the stockquote proxy

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">  
   <soapenv:Header/>  
   <soapenv:Body>  
    <ser:getQuote>  
      <!--Optional:-->  
      <ser:request>  
       <!--Optional:-->  
       <xsd:symbol>wso2</xsd:symbol>  
      </ser:request>  
    </ser:getQuote>  
   </soapenv:Body>  
 </soapenv:Envelope>  

It will make a HTTP GET call to the URL and get back the response
http://localhost:8080/stockquote/getQuote?symbol=wso2

Then proxy service send response back to the client

No comments:

Post a Comment