A SOAP API (Simple Object Access Protocol API) is a protocol-based API that allows communication between applications over a network. Unlike REST, which is an architectural style, SOAP is a strict protocol with well-defined standards for message formatting, transport, and error handling. SOAP APIs use XML as their message format and rely on SOAP envelopes to structure requests and responses.
SOAP was designed for enterprise-level systems requiring high security, reliability, and formal standards for communication.
SOAP APIs follow a strict messaging structure:
SOAP Request:
A client sends a request to the server in the form of an XML document, wrapped in a SOAP envelope. The envelope contains the request details, including the action to perform and any necessary parameters.
SOAP Response:
The server processes the request and responds with another XML document wrapped in a SOAP envelope. This contains the result or error message.
SOAP APIs often use the HTTP/HTTPS protocol for transport, but they can also work with other protocols like SMTP or TCP.
XML-Based Messaging:
SOAP exclusively uses XML for its request and response messages, ensuring strict structure and compatibility across different systems.
Protocol-Driven:
SOAP is a formal protocol with strict rules for structuring messages, error handling, and security.
Transport Independence:
While SOAP commonly uses HTTP/HTTPS, it can also operate over other protocols like SMTP, FTP, or TCP.
WS-Security:
SOAP APIs include robust security features (e.g., encryption, authentication) via standards like WS-Security, making them ideal for sensitive data.
Service Definition:
SOAP APIs require a WSDL (Web Services Description Language) document. The WSDL is an XML-based contract that defines the service, its available methods, and the structure of requests and responses.
Stateful and Stateless:
Unlike REST, which is stateless, SOAP can support both stateless and stateful operations, depending on the system requirements.
A SOAP message is an XML document divided into several parts:
<soap:Envelope xmlns:soap="<http://schemas.xmlsoap.org/soap/envelope/>">
<soap:Header>
<!-- Optional Header Information -->
</soap:Header>
<soap:Body>
<GetWeather>
<City>New York</City>
</GetWeather>
</soap:Body>
</soap:Envelope>