SOAP Web Services

SOAP (Simple Object Access Protocol) is a protocol used for exchanging structured information in web services. At the core of any SOAP-based interaction is the SOAP message, which is an XML-based document that carries the data between a client and a server. A well-structured SOAP message is crucial for the proper functioning of web services, ensuring that both client and server can communicate effectively, regardless of platform or language.

Overview of SOAP Message Structure

A SOAP message is an XML document with a defined structure. The message consists of four main parts:

  1. Envelope: The root element that defines the beginning and end of the SOAP message.
  2. Header (Optional): Used to carry meta-information such as security, transaction management, etc.
  3. Body: Contains the actual message content (data or parameters).
  4. Fault (Optional): Used to carry error or status information.

Here is a high-level overview of a SOAP message structure:

				
					<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>...</soapenv:Header>
    <soapenv:Body>...</soapenv:Body>
</soapenv:Envelope>
				
			

The above structure contains an envelope that wraps a header (optional) and a body (required), forming the complete SOAP message.

SOAP Envelope

The Envelope is the root element of the SOAP message, and it is mandatory. It defines the namespace and sets the boundaries for the message. The envelope contains two primary child elements:

  • The Header (optional)
  • The Body (mandatory)

The envelope element ensures that the message is properly formatted and helps to avoid potential conflicts in data structure.

Example of SOAP Envelope:

				
					<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body/>
</soapenv:Envelope>

				
			

Here, the xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" attribute specifies the namespace used for SOAP.

SOAP Header (Optional)

The Header is an optional part of the SOAP message. It contains metadata about the message, such as security information, authentication tokens, or transaction details. This section is useful for any additional control information that needs to be passed between the client and server without affecting the message body.

Multiple header entries can exist within the header element. These entries are typically processed by intermediate nodes before the message reaches its final destination.

Example of a SOAP Header:

				
					<soapenv:Header>
    <auth:AuthHeader xmlns:auth="http://example.com/auth">
        <auth:Username>admin</auth:Username>
        <auth:Password>secret</auth:Password>
    </auth:AuthHeader>
</soapenv:Header>
				
			

In this example, the header includes authentication details (username and password), which can be processed by the server to authenticate the request.

SOAP Body

The Body is the mandatory element in a SOAP message, and it contains the actual message data that is sent to the recipient. This data could be a request, response, or any other information depending on the operation. The body element is crucial as it carries the operation details in the form of XML elements.

The structure of the body depends on the operation being performed. The client sends a request, and the server responds with a result, both contained within the body of the respective SOAP messages.

Example of a SOAP Body:

				
					<soapenv:Body>
    <calc:Add xmlns:calc="http://example.com/calculator">
        <calc:number1>5</calc:number1>
        <calc:number2>10</calc:number2>
    </calc:Add>
</soapenv:Body>

				
			

In this example, the client is requesting the “Add” operation, passing two numbers (5 and 10) as parameters. The server will process this request and send back a response in a similar structure.

Expected Output (Response):

				
					<soapenv:Body>
    <calc:AddResponse xmlns:calc="http://example.com/calculator">
        <calc:result>15</calc:result>
    </calc:AddResponse>
</soapenv:Body>

				
			

The result of the addition (15) is returned in the body of the SOAP response.

SOAP Fault (Optional)

The Fault element is an optional part of the SOAP message. It is used to provide information about errors or issues that occurred during the processing of the message. When an error occurs, instead of returning a successful response, the server returns a fault message detailing the error.

The fault element consists of the following parts:

  • faultcode: A code representing the type of error.
  • faultstring: A human-readable explanation of the fault.
  • faultactor: (Optional) The actor responsible for the fault (typically used in multi-node environments).
  • detail: (Optional) Additional details about the error.

Example of a SOAP Fault:

				
					<soapenv:Body>
    <soapenv:Fault>
        <faultcode>soapenv:Client</faultcode>
        <faultstring>Invalid input parameter</faultstring>
    </soapenv:Fault>
</soapenv:Body>

				
			

In this example, the fault indicates that the client provided invalid input parameters, and the server could not process the request.

Output:

The client will receive this fault message instead of a successful response, and it will be able to understand that the input parameters need to be corrected.

Detailed Example of a Complete SOAP Request and Response

Let’s put together everything we’ve learned so far in a complete example. Below is a full SOAP request and its corresponding response.

SOAP Request:

				
					POST /calculatorService HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: 355

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:calc="http://example.com/calculator">
    <soapenv:Header/>
    <soapenv:Body>
        <calc:Add>
            <calc:number1>10</calc:number1>
            <calc:number2>20</calc:number2>
        </calc:Add>
    </soapenv:Body>
</soapenv:Envelope>

				
			

SOAP Response:

				
					HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 412

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:calc="http://example.com/calculator">
    <soapenv:Body>
        <calc:AddResponse>
            <calc:result>30</calc:result>
        </calc:AddResponse>
    </soapenv:Body>
</soapenv:Envelope>
				
			

In this scenario:

  • The client sends a request to the server to add two numbers (10 and 20).
  • The server processes the request and returns the result (30) in the response.

Transport Protocols for SOAP

SOAP is designed to be transport-independent, which means it can be used with different transport protocols. However, HTTP is the most commonly used protocol for SOAP messages. Below is how SOAP works over HTTP:

  • SOAP over HTTP: SOAP messages are often encapsulated in HTTP requests (typically POST) and HTTP responses. This makes it easy to work with SOAP in web applications, as HTTP is widely supported.
  • SOAP over SMTP: In some scenarios, SOAP messages can also be transmitted over SMTP (Simple Mail Transfer Protocol) for email-based messaging systems.

SOAP over HTTP Example (Request):

				
					POST /service HTTP/1.1
Host: www.example.com
Content-Type: text/xml; charset=utf-8
Content-Length: 348

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:web="http://www.example.com/webservice">
    <soapenv:Body>
        <web:GetData>
            <web:id>123</web:id>
        </web:GetData>
    </soapenv:Body>
</soapenv:Envelope>

				
			

The client sends a POST request to the server with a SOAP message in the body. The server responds in a similar fashion, encapsulating its response in an HTTP response.

Advantages of SOAP Message Structure

  • Well-defined Structure: SOAP provides a rigid and well-defined message structure, ensuring compatibility and standardization.
  • Error Handling: The SOAP Fault element provides a clear and standardized way to handle errors and inform clients of issues.
  • Protocol Flexibility: Although HTTP is the most common protocol, SOAP can work over multiple transport protocols, giving it flexibility in different network environments.
  • Platform and Language Agnostic: SOAP messages are written in XML, making them usable in any programming language and on any platform.

Understanding the structure of SOAP messages is crucial when working with SOAP-based web services. From the mandatory Envelope and Body elements to the optional but important Header and Fault elements, each part plays a key role in the communication process. By adhering to the strict structure provided by SOAP, developers can ensure smooth and reliable data exchanges across platforms. Happy Coding!❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India