Saturday, July 10, 2010

Sending XML content as a value in name value pair GET request using WSO2 ESB

In this blog post I am describing on how to invoke a RESTful service which required a GET request with name value pairs in the request including XML content to be there in one of the value. First I will describe on how to invoke a normal RESTful service via ESB.

If you want to send a request to a service like this

http://service.endpoint/ServiceName?name1=value1&name2=value2...

First create an address endpoint by keeping the address of http://service.endpoint and make the endpoint type as REST/GET if you are using the UI to configure ESB. So the Endpoint configuration will looks like this.

<endpoint xmlns="http://ws.apache.org/ns/synapse">
<address uri="http://service.endpoint/" format="get" >
<suspendOnFailure>
<initialDuration>10000</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>30000</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
<retryDelay>0</retryDelay>
</markForSuspension>
</address>
</endpoint>


You can simply send the content of the request like this

<ServiceName>
<name1>value1</name1>
<name2>value2</name2>
</ServiceName>

No matter what type of request you are sending because when WSO2 ESB start processing the request it sees only a soap message after getting called message builders and message formatters. So you can see that during the endpoint creation I have remove the ServiceName part from the endpoint since we are sending that in the request and when we mark the endpoint format=get Axis2 will add that part in to the HTTP GET request and append the name value pairs to the request and send the message properly.

So this scenario is very simple and now let's see a case where you need to send something more complex in a HTTP GET request. I came across a scenario where we need to send an encoded xml in the GET request including some of the normal name value pairs.

So the HTTP GET request looks like this,
Ex:
http://service.endpoint/ServiceName?name1=value1&name2=value2&name3=<encoded xml content>

In this kind of a scenario you might get confused on how do you have to send the request. First create an endpoint as if the above configuration by making the format as get. Then,if you are in a position that you can change the incoming request you can simply send the request content like this.

<ServiceName>
<name1>value1</name1>
<name2>value2</name2>
<name3><![CDATA[xmlcontent]]></name3>
</ServiceName>

When ESB send the message to the REST endpoint Axis2 will encode the content inside the CDATA and send the message properly as I have showed above. If you are not capable of sending the message with CDATA content you can simply send the required elements in the incoming request and do an XSLT transformation to create the message as above.



1 comment :

Look Rio said...

Works!!!

Thank you very much!