π Notes: WebService, REST, REST API, SOAP
π 1. Web Service
A software system designed for interoperable machine-to-machine communication over a network.
π Key Features:
- Uses HTTP/HTTPS for communication.
- Operates on XML or JSON for data exchange.
- Enables interoperability between applications across different platforms/languages.
π‘ Types of Web Services:
- SOAP (Simple Object Access Protocol)
- REST (Representational State Transfer)
π REST Architecture (Representational State Transfer)
An architectural style for designing scalable, flexible, and performant distributed systems using HTTP.
π Key Characteristics:
-
Resource-Based:
- Resources uniquely identified by URIs.
- Example:
/users/123
β Access user with ID 123.
/orders/456
β Access order with ID 456.
-
HTTP Verbs for CRUD Operations:
- GET: Retrieve a resource.
- POST: Create a resource.
- PUT: Update a resource.
- DELETE: Remove a resource.
-
Stateless Communication:
- Each request contains all necessary context.
- Server does not store client state.
-
Representation of Resources:
- Formats: JSON (most common), XML, HTML.
-
Uniform Interface:
- Consistent APIs with standard response codes:
200
: Success.404
: Not found.500
: Server error.
- Consistent APIs with standard response codes:
-
Cacheable:
- Responses can be cached to improve performance.
-
Client-Server Separation:
- Clear separation of frontend (client) and backend (server).
π Advantages:
- Lightweight, flexible, and simple to implement.
- Works seamlessly for web and mobile applications.
- Scalable due to statelessness.
βοΈ Disadvantages:
- Lacks built-in security; requires additional measures.
- Statelessness can increase complexity for workflows.
π§Ό SOAP Protocol (Simple Object Access Protocol)
A protocol defining strict standards for exchanging structured information in web services.
π Key Features:
-
XML-Based Messaging:
- Example Request:
πXMLβ οΈ
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getUserDetails> <userId>123</userId> </getUserDetails> </soap:Body> </soap:Envelope>
β οΈLanguage "xml" may have limited syntax highlighting support
- Example Request:
-
Protocol Independence:
- Can use HTTP, SMTP, TCP, etc.
-
WSDL (Web Services Description Language):
- Describes SOAP services, operations, parameters, and endpoints.
-
Built-In Security (WS-Security):
- Supports encryption, authentication, and message integrity.
-
Stateful Support:
- Can maintain state across requests.
π How SOAP Works:
- Request:
Client sends an XML-formatted SOAP request. - Processing:
Server processes request based on WSDL. - Response:
Server responds with an XML-formatted SOAP message.
π Advantages:
- Language and protocol-neutral.
- Reliable with built-in error handling.
- Secure with WS-Security.
- Ideal for enterprise-level applications.
βοΈ Disadvantages:
- Heavy payload due to XML.
- Complexity in setup and maintenance.
- Slower performance compared to REST.
βοΈ REST vs SOAP: A Comparison
- Architecture/Protocol:
- REST: Architectural style (flexible).
- SOAP: Protocol (strict rules).
- Message Format:
- REST: Supports JSON, XML, and HTML.
- SOAP: Only supports XML.
- Communication:
- REST: Uses HTTP only.
- SOAP: Can use HTTP, SMTP, TCP, etc.
- State Management:
- REST: Stateless (no session management).
- SOAP: Stateful (optional, maintains session state).
- Ease of Use:
- REST: Simple and lightweight.
- SOAP: Complex and feature-rich.
- Security:
- REST: Requires additional security measures.
- SOAP: Built-in WS-Security for encryption, authentication, and integrity.
- Performance:
- REST: Faster (lighter message format).
- SOAP: Slower (due to XML processing overhead).
- Use Case:
- REST: Ideal for web/mobile apps, lightweight systems.
- SOAP: Best for enterprise apps, complex workflows.
βοΈ Difference Between REST and REST API
-
REST (Representational State Transfer):
- Architectural Style: Itβs an architectural pattern for designing scalable, flexible, and performant distributed systems.
- Stateless: Every request contains all information for the server to process.
- Resource-Based: Resources are identified via URIs (e.g.,
/users/123
). - Standardized HTTP Methods: GET, POST, PUT, DELETE, etc., for CRUD operations.
- Representation: Commonly uses JSON, XML, or HTML to represent resources.
- Caching: Responses can be cached for better performance.
-
REST API (Representational State Transfer API):
- Implementation of REST: A web service that follows the REST architecture.
- Provides Endpoints: Exposes resources for interaction through defined HTTP methods.
- Facilitates Communication: REST APIs allow systems to communicate using REST principles over HTTP/HTTPS.
- Lightweight & Scalable: Easily implementable and scalable for web and mobile applications.
Key Difference: REST is the design philosophy (architecture), whereas a REST API is the actual implementation of that architecture, typically providing endpoints for client-server communication.