SOAP Body — The Payload Element for Service Operations
In this tutorial, you will learn about SOAP Body. We cover key concepts, practical examples, and best practices to help you master this topic.
The SOAP Body is a mandatory child element of the Envelope that contains the actual payload of the message — the specific request data or response data for the invoked operation.
What You'll Learn
- The structure and requirements of the SOAP Body
- How request and response messages are structured
- Error handling with SOAP Fault in the Body
Why It Matters
The Body contains the actual business data. Understanding Body structure is essential for building correct SOAP request and response messages.
Body Structure
- Request Body — Contains the operation name as the first child element with input parameters
- Response Body — Contains the response wrapper element with output data
- Fault Body — Contains a single
Faultelement for error reporting
Code Examples
<!-- SOAP Request Body -->
<soap:Body>
<p:PlaceOrder xmlns:p="http://example.com/orders">
<p:CustomerID>12345</p:CustomerID>
<p:Items>
<p:Item>
<p:ProductID>ABC-789</p:ProductID>
<p:Quantity>2</p:Quantity>
</p:Item>
</p:Items>
<p:ShippingAddress>
<p:Street>123 Main St</p:Street>
<p:City>New York</p:City>
<p:ZIP>10001</p:ZIP>
</p:ShippingAddress>
</p:PlaceOrder>
</soap:Body>
<!-- SOAP Response Body -->
<soap:Body>
<p:PlaceOrderResponse xmlns:p="http://example.com/orders">
<p:OrderID>ORD-2024-7890</p:OrderID>
<p:Status>Confirmed</p:Status>
<p:EstimatedDelivery>2024-01-20</p:EstimatedDelivery>
</p:PlaceOrderResponse>
</soap:Body>
# Extracting data from SOAP Body
root = ET.fromstring(soap_response)
ns = {'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
'p': 'http://example.com/orders'}
body = root.find('soap:Body', ns)
response = body.find('p:PlaceOrderResponse', ns)
order_id = response.find('p:OrderID', ns).text
status = response.find('p:Status', ns).text
print(f"Order {order_id}: {status}")
Common Mistakes
1. Including Multiple Body Elements
The Body can only contain one child element (the operation payload) plus an optional Fault.
2. Forgetting the Operation Wrapper Element
Parameters must be wrapped inside the operation element.
3. Missing Required Parameters
If the WSDL defines required parameters, omitting them causes a validation fault.
4. Incorrect Data Types
The Body must use data types matching the XSD schema defined in the WSDL.
5. Mixing Request and Response Structures
Request and response use different element names (e.g., PlaceOrder vs PlaceOrderResponse).
Practice Questions
- How many child elements can the SOAP Body contain?
- What is the naming convention for response elements?
- How does the SOAP Fault relate to the Body?
- What happens if the Body contains an unknown operation?
- Can the Body contain binary data?
Answers:
- One operation element plus an optional Fault element.
- Usually the request name with "Response" appended (e.g.,
PlaceOrderResponse). - The Fault is placed inside the Body element for error conditions.
- The server returns a SOAP Fault with
Clientcode. - Yes, using base64 encoding within XML elements.
Challenge: Design the SOAP request and response Body for a shipping rate calculator. Include origin, destination, weight, and package dimensions in the request.
FAQ
What's Next
Learn about SOAP Fault handling for error responses, then explore WS-Addressing and WS-Security.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro