Wednesday, October 15, 2008

Apache Synapse/WSO2 ESB calling a REST Endpoint

One of the features of Apache Synapse and the WSO2 ESB (Which is Apache Synapse + Monitoring + Management + WSO2 Registry Integration) is that it can change message formats.

There was a question on the ESB-USER mailing list recently asking how you could receive a SOAP message and send out a REST message (HTTP POST serialized as application/x-www-form-urlencoded) to another service. In order to do this you simply need to set two properties on the message. The HTTP_Method to be used and the messageType to be used. The following is a complete synapse.xml

<syn:definitions xmlns:syn="http://ws.apache.org/ns/synapse">

<syn:registry provider="org.wso2.esb.registry.ESBRegistry">

<syn:parameter name="root">file:registry/</syn:parameter>

</syn:registry>

<syn:proxy name="RESTProxy" startOnLoad="true">

<syn:target>

<syn:endpoint name="RESTEndpoint">

<syn:address uri="http://localhost:7763/services/keith/test/foo"/>

</syn:endpoint>

<syn:inSequence>

<syn:property name="messageType" value="application/x-www-form-urlencoded" scope="axis2"/>

<syn:property name="HTTP_METHOD" value="post" scope="axis2"/>

</syn:inSequence>

</syn:target>

</syn:proxy>

<syn:sequence name="main">

<syn:send/>

</syn:sequence>

<syn:sequence name="fault">

<syn:log/>

</syn:sequence>

</syn:definitions>


Here is the request sent into the ESB:

POST /soap/RESTProxy/mediate HTTP/1.1

Content-Type: application/soap+xml; charset=UTF-8; action="urn:anonOutInOp"

User-Agent: WSO2 Mashup Server - Version 1.5.1

Host: 127.0.0.1

Content-Length: 191



<?xml version='1.0' encoding='UTF-8'?>

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">

<soapenv:Body>

<foo>

<param>keith</param>

</foo>

</soapenv:Body>

</soapenv:Envelope>


Here is its response (or Request sent out to the REST service):

POST http://localhost:7763/services/keith/test/foo HTTP/1.1
Host: 127.0.0.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8;action="urn:anonOutInOp";
Transfer-Encoding: chunked
Connection: Keep-Alive
User-Agent: Synapse-HttpComponents-NIO

b
param=keith
0

1 comment:

Unknown said...

Hi Keith,

What would I need to do if I want to receive POST from a form and send POST to the endpoint?

Thankyou