Thursday 17 May 2007

Creating POST requests with curl for Axis2 REST service

Axis2 allows to create REST services as easily as SOAP services. By default each
SOAP service has a REST counterpart automatically available.

The responses for REST requests served by Axis2 are the same as SOAP responses but
are missing the enclosing SOAP envelope (this is because REST is handled inte
rnally like a SOAP request). Because of this the responses are currently missing
the REST feature to return links to other resources in the response.

But creating REST requests is somewhat easier than SOAP requests. This article
describes an example where a REST request is sent with curl.

Axis2 uses document-style services by default. The following excerpt of the
webservice WSDL which was used shows the XML schema for the data to be transmitted:


<xs:schema xmlns:ns="http://impl.service.banapple.de/xsd" attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="http://impl.serv ice.banapple.de/xsd">
<xs:element name="createContact">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="incomingData" nillable="true" type="ns10:IncomingData" />
<xs:element maxOccurs="unbounded" name="foreignKeys" nillable="true" type="ns10:IncomingForeignKey" />
<xs:element name="incomingCreator" nillable="true" type="ns10:IncomingCreator" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="createContactResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="return" nillable="true" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema xmlns:ax242="http://bean.model.banapple.de/xsd" xmlns:ax283="http://lang.java/xsd" xmlns:ax244="http://util.java/xsd" attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="http://bean.model.banapple.de/xsd">
<xs:import namespace="http://util.java/xsd" />
<xs:import namespace="http://lang.java/xsd" />
<xs:element name="IncomingData" type="ns10:IncomingData" />
<xs:complexType name="IncomingData">
<xs:sequence>
<xs:element name="creationDate" type="xs:long" />
<xs:element name="dataTypeName" nillable="true" type="xs:string" />
<xs:element name="dataValue" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="IncomingForeignKey" type="ns10:IncomingForeignKey" />
<xs:complexType name="IncomingForeignKey">
<xs:sequence>
<xs:element name="foreignKeyTypeName" nillable="true" type="xs:string" />
<xs:element name="foreignKeyValue" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="IncomingCreator" type="ns10:IncomingCreator" />
<xs:complexType name="IncomingCreator">
<xs:sequence>
<xs:element name="name" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>


Two namespaces are used:
http://impl.service.banapple.de/xsd for the element describing the
operation,
http://bean.model.banapple.de/xsd for the parameters of the operation.

The example payload for the request is the following:


<createContact
xmlns="http://impl.service.banapple.de/xsd"
xmlns:bean="http://bean.model.banapple.de/xsd">
<bean:incomingData>
<creationDate>0</creationDate>
<dataTypeName>CUSTOMER_ID</dataTypeName>
<dataValue>300042045</dataValue>
</bean:incomingData>
<foreignKeys>
<foreignKeyTypeName>UCID</foreignKeyTypeName>
<foreignKeyValue>1</foreignKeyValue>
</foreignKeys>
<incomingCreator>
<name>Foobar</name>
</incomingCreator>
</createContact>


The payload was stored in a file createcontact.xml and sent to the
REST service with curl with the command:


curl -H "Content-Type: text/xml; charset=UTF-8"
--data-binary @createcontact.xml
http://localhost:8080/contactdb/rest/DataReceiverService/createContact



It is important to sent the Content-Type header otherwise the REST
service will produce an error response with the not very helpful message

Required element null defined in the schema can not be found in the request

No comments: