Explain the concept of RESTful API
Question
Answer
REST (Representational State Transfer) is an architectural style for designing networked applications. RESTful API is an API that follows REST principles.
REST Principles:
1. Stateless: Each request from client to server must contain all information needed to understand the request
2. Client-Server: Separation of concerns between client and server
3. Cacheable: Responses should be cacheable to improve performance
4. Uniform Interface: Consistent interface between components
5. Layered System: Architecture can be composed of hierarchical layers
6. Code on Demand (optional): Server can send executable code to client
HTTP Methods:
- GET: Retrieve data
- POST: Create new resource
- PUT: Update entire resource
- PATCH: Partial update
- DELETE: Remove resource
REST vs SOAP:
| REST |
| SOAP |
| ------ |
| ------ |
| Uses HTTP/HTTPS |
| Uses HTTP, SMTP, TCP |
| JSON, XML, HTML |
| XML only |
| Stateless |
| Can be stateful |
| Lightweight |
| Heavyweight |
| Easy to learn |
| More complex |
Example RESTful Endpoint:
GET /api/users/123
POST /api/users
PUT /api/users/123
DELETE /api/users/123