SOAP (Simple Object Access Protocol) is a protocol used for exchanging structured information in web services. It relies on XML for message format and usually operates over HTTP or SMTP. One of the key aspects of SOAP is its use of various data types and serialization methods to transmit data effectively. This article provides practical examples of SOAP API data types and serialization to help you understand their application.
In many web services, simple data types such as integers and strings are frequently used. This example illustrates the use of a SOAP API to calculate the area of a rectangle using simple data types.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rect="http://example.com/rectangle">
<soapenv:Header/>
<soapenv:Body>
<rect:CalculateArea>
<rect:Length>5</rect:Length>
<rect:Width>3</rect:Width>
</rect:CalculateArea>
</soapenv:Body>
</soapenv:Envelope>
The response from the server would be:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<rect:AreaResult>15</rect:AreaResult>
</soapenv:Body>
</soapenv:Envelope>
Complex data types are often used to encapsulate multiple properties. In this example, we’ll see how to send a user profile with multiple attributes using a SOAP API.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:user="http://example.com/user">
<soapenv:Header/>
<soapenv:Body>
<user:CreateUser>
<user:Profile>
<user:Name>John Doe</user:Name>
<user:Email>john.doe@example.com</user:Email>
<user:Age>30</user:Age>
</user:Profile>
</user:CreateUser>
</soapenv:Body>
</soapenv:Envelope>
The server response would look like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<user:UserId>12345</user:UserId>
<user:CreationStatus>Success</user:CreationStatus>
</soapenv:Body>
</soapenv:Envelope>
When dealing with multiple items, array data types can be very useful. This example demonstrates how a SOAP API can handle the submission of a list of product IDs.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://example.com/product">
<soapenv:Header/>
<soapenv:Body>
<prod:GetProductDetails>
<prod:ProductIds>
<prod:Id>101</prod:Id>
<prod:Id>102</prod:Id>
<prod:Id>103</prod:Id>
</prod:ProductIds>
</prod:GetProductDetails>
</soapenv:Body>
</soapenv:Envelope>
The response would be structured as follows:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<prod:ProductDetails>
<prod:Detail>
<prod:Id>101</prod:Id>
<prod:Name>Widget</prod:Name>
</prod:Detail>
<prod:Detail>
<prod:Id>102</prod:Id>
<prod:Name>Gadget</prod:Name>
</prod:Detail>
</prod:ProductDetails>
</soapenv:Body>
</soapenv:Envelope>