Friday, February 14, 2014

WSO2 ESB - Access query parameters in REST API

Not only SOAP, WSO2 ESB also provides the REST support, you can define a REST API within wso2 ESB or you can define this using WSO2 Developer Studio and then deploy it to ESB using .car file. Sample 800[1] of WSO2 ESB docs describes how you can access a REST API, however the sample only describes uri-parameters style, not the query string parameter style. If you access it as described in [1] i.e http://127.0.0.1:8280/stockquote/view/IBM style, the sample would work.
However if you want to access the service like http://127.0.0.1:8280/stockquote/view?Name=IBM&Param2=dummy format, you have do to some minor modifications to the sample[1] to make it work. Its only a single line, just replace the line #4 as follows.

 
Here there is no usage of para2 in this example, but i have just added this to demonstrate how you can access multiple parameters. Note the log mediator line #6, which is use to access the query parameters and log them. You can use property mediators to get the query parameters using expressions.


    
    

Here is the modified code for the sample 800

   
      
         
            
            
         
         
            
               
                  
                     $1
                  
               
            
            
               
            
         
         
Just add this API to esb and invoke using curl or simply you can use the browser. Paste http://127.0.0.1:8280/stockquote/view?Name=IBM&Param2=dummy in the address bar and hit 'Enter'. You will see the StockQuote response for the symbol IBM. If you looked at the server console you may see the following log.

INFO - LogMediator To: /stockquote/view?Name=IBM&Param2=dummy, MessageID: urn:uuid:1489b1cf-9a43-4fc6-9904-c6cd2244c87f, Direction: request, SymboValue = IBM, Param2Value = dummy 

[1] - http://docs.wso2.org/display/ESB480/Sample+800:+Introduction+to+REST+API