XML SOAP
Simple Object Access Protocol for web services
๐งผ What is SOAP?
SOAP (Simple Object Access Protocol) is a messaging protocol for exchanging structured information in web services. It uses XML format and works over HTTP, SMTP, and other protocols for reliable communication.
<!-- Basic SOAP Message -->
<soap:Envelope>
<soap:Body>
<GetUser>...</GetUser>
</soap:Body>
</soap:Envelope>
SOAP Message Structure
Envelope
Root element wrapping the message
<soap:Envelope
xmlns:soap="...">
</soap:Envelope>
Header
Optional metadata and authentication
<soap:Header>
<auth>token123</auth>
</soap:Header>
Body
Main message content and data
<soap:Body>
<GetUserRequest>...</GetUserRequest>
</soap:Body>
Fault
Error information in responses
<soap:Fault>
<faultcode>...</faultcode>
</soap:Fault>
๐น Simple SOAP Request
A basic SOAP request to retrieve user information. The envelope contains the body with the actual request data sent to the web service.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:user="http://example.com/users">
<soap:Body>
<user:GetUserRequest>
<user:userId>12345</user:userId>
</user:GetUserRequest>
</soap:Body>
</soap:Envelope>
Request Flow:
๐ค Client sends SOAP request to service endpoint
โ๏ธ Server processes GetUserRequest operation
๐ฅ Server returns SOAP response with user data
๐น SOAP Response
The server's SOAP response containing the requested user data. Responses follow the same envelope structure with the result in the body.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:user="http://example.com/users">
<soap:Body>
<user:GetUserResponse>
<user:User>
<user:id>12345</user:id>
<user:name>John Doe</user:name>
<user:email>[email protected]</user:email>
<user:status>active</user:status>
</user:User>
</user:GetUserResponse>
</soap:Body>
</soap:Envelope>
Response Data:
๐น SOAP with Header
SOAP headers carry metadata like authentication tokens, transaction IDs, or routing information. Headers are processed before the body content.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:auth="http://example.com/auth"
xmlns:prod="http://example.com/products">
<soap:Header>
<auth:Authentication>
<auth:token>abc123xyz789</auth:token>
<auth:timestamp>2024-10-16T10:30:00Z</auth:timestamp>
</auth:Authentication>
</soap:Header>
<soap:Body>
<prod:GetProductRequest>
<prod:productId>101</prod:productId>
</prod:GetProductRequest>
</soap:Body>
</soap:Envelope>
Common Header Uses:
- Authentication: API keys, tokens, credentials
- Transaction: Transaction IDs for tracking
- Routing: Message routing information
- Security: Digital signatures, encryption
๐น SOAP Fault (Error Handling)
When errors occur, SOAP returns a Fault element in the body. This provides structured error information for proper error handling.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>User not found</faultstring>
<detail>
<error xmlns="http://example.com/errors">
<code>404</code>
<message>No user exists with ID 99999</message>
<timestamp>2024-10-16T10:30:00Z</timestamp>
</error>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Fault Codes:
- soap:Client: Client error (bad request)
- soap:Server: Server error (processing failed)
- soap:VersionMismatch: Invalid SOAP version
- soap:MustUnderstand: Required header not understood
๐น Complete SOAP Example: Order Service
A comprehensive example showing order creation with authentication and detailed response:
<!-- SOAP Request -->
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:auth="http://example.com/auth"
xmlns:order="http://example.com/orders">
<soap:Header>
<auth:Credentials>
<auth:apiKey>key123</auth:apiKey>
</auth:Credentials>
</soap:Header>
<soap:Body>
<order:CreateOrderRequest>
<order:customerId>5678</order:customerId>
<order:items>
<order:item>
<order:productId>101</order:productId>
<order:quantity>2</order:quantity>
</order:item>
<order:item>
<order:productId>102</order:productId>
<order:quantity>1</order:quantity>
</order:item>
</order:items>
</order:CreateOrderRequest>
</soap:Body>
</soap:Envelope>
<!-- SOAP Response -->
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:order="http://example.com/orders">
<soap:Body>
<order:CreateOrderResponse>
<order:orderId>ORD-2024-001</order:orderId>
<order:status>confirmed</order:status>
<order:totalAmount>149.98</order:totalAmount>
<order:createdAt>2024-10-16T10:30:00Z</order:createdAt>
</order:CreateOrderResponse>
</soap:Body>
</soap:Envelope>
๐น SOAP vs REST
Understanding the differences between SOAP and REST web services:
SOAP Advantages:
- Strict standards: Well-defined protocol and structure
- Built-in error handling: Standardized fault mechanism
- Security: WS-Security for enterprise needs
- ACID compliance: Transaction support
REST Advantages:
- Simplicity: Easier to implement and use
- Performance: Less overhead, faster processing
- Flexibility: Supports multiple formats (JSON, XML)
- Caching: Better HTTP caching support
๐น SOAP HTTP Request
How SOAP messages are sent over HTTP:
POST /api/users HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: 350
SOAPAction: "http://example.com/users/GetUser"
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:user="http://example.com/users">
<soap:Body>
<user:GetUserRequest>
<user:userId>12345</user:userId>
</user:GetUserRequest>
</soap:Body>
</soap:Envelope>
HTTP Headers:
Content-Type: text/xml or application/soap+xml
SOAPAction: Identifies the operation
Method: Always POST for SOAP