SOAP API Data Types Examples

Explore practical examples of SOAP API data types and serialization to enhance your understanding.
By Jamie

Understanding SOAP API Data Types and Serialization

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.

Example 1: Simple Data Types in SOAP API

Context

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>

Notes

  • The Length and Width are simple data types of the integer type.
  • The response provides the calculated area, demonstrating the serialization of the result.

Example 2: Complex Data Types in SOAP API

Context

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>

Notes

  • The Profile in the request is a complex data type containing multiple attributes (Name, Email, and Age).
  • This example shows how to serialize a user profile into a single request.

Example 3: Array Data Types in SOAP API

Context

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>

Notes

  • The ProductIds element demonstrates the use of an array data type to send multiple product IDs.
  • The response includes details for each product, showcasing how the system handles serialized arrays.