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

6 comments:

Anonymous said...

Dear Susinda,

Could you please let me know how we can deploy the "SimpleStockQuoteService" example in order to call the your suggested web services?

Please guide me ASAP

regards,
Neha

Susinda Perera said...

Please follows the "Deploying sample back-end services" section in https://docs.wso2.com/display/ESB481/Setting+Up+the+ESB+Samples

Anonymous said...

Can you please help me in API Manager in Publisher how to get read request query parameter from api and map it to endpoint REST service as request query in GET call.
And for POST how to post request body in endpoint REST service.
I am really stuck in this points please help to sort it out.

Susinda Perera said...

To do the same with API's follow
http://soatutorials.blogspot.com/2014/03/wso2-esb-creating-response-for-get.html

Shilpa said...

oHOw to Pass a URl parameter from API manager to ESB Proxy

Susinda Perera said...

Pls refer https://docs.wso2.com/display/ESB480/Sample+800:+Introduction+to+REST+API
See how the {symbol} and ('uri.var.symbol') is defined, hope you can get some idea.

Thanks