RESTful Web Services
πΉ REST (Representational State Transfer)
Section titled βπΉ REST (Representational State Transfer)β- REST is an architectural style for building lightweight web services.
- REST APIs expose resources over HTTP using standard verbs (GET, POST, PUT, DELETE) and URIs.
- REST is often used to implement SOA or microservices in modern systems.
πΉ REST Principles (Constraints)
Section titled βπΉ REST Principles (Constraints)β- Client-Server separation
- Stateless requests
- Cacheable responses
- Uniform Interface (resources via URIs, verbs for actions)
- Layered System (intermediaries allowed)
- Resource-based (everything is a resource, not a method)
πΉ HTTP Methods in REST
Section titled βπΉ HTTP Methods in RESTβ| Method | Usage | Example |
|---|---|---|
| GET | Retrieve a resource | GET /users/1 |
| POST | Create a new resource | POST /users |
| PUT | Update/replace a resource | PUT /users/1 |
| PATCH | Partially update a resource | PATCH /users/1 |
| DELETE | Remove a resource | DELETE /users/1 |
| OPTIONS | List available operations | OPTIONS /users |
| HEAD | Same as GET, but no body | HEAD /users/1 |
πΉ Common HTTP Status Codes
Section titled βπΉ Common HTTP Status Codesββ Success (2xx)
200 OKβ Success (GET/PUT/DELETE).201 Createdβ Resource created (POST).202 Acceptedβ Async processing.204 No Contentβ Success, no body.
β οΈ Client Errors (4xx)
400 Bad Requestβ Invalid input.401 Unauthorizedβ Auth required/failed.402 Payment Requiredβ Payment Required.403 Forbiddenβ No permission.404 Not Foundβ Resource not found.405 Method Not Allowedβ Wrong HTTP method.409 Conflictβ Duplicate or state conflict.422 Unprocessable Entityβ Validation error.429 Too Many Requestsβ Rate-limited.
π¨ Server Errors (5xx)
500 Internal Server Errorβ Generic failure.502 Bad Gatewayβ Invalid upstream response.503 Service Unavailableβ Temporary downtime.504 Gateway Timeoutβ Upstream timed out.
πΉ REST Best Practices
Section titled βπΉ REST Best Practicesβ- Use nouns, not verbs in URIs β
/users/1/ordersinstead of/getUserOrders. - Use plural resource names β
/productsnot/product. - Support pagination/filtering β
/products?page=2&limit=20. - Return structured error messages (with code, message, details).
- Use versioning β
/api/v1/users. - Secure with HTTPS, OAuth2, JWT.
πΉ Example REST API
Section titled βπΉ Example REST APIβ
|
|
πΉ Common Interview Qs on REST
Section titled βπΉ Common Interview Qs on RESTβDifference between PUT and PATCH?
PUTreplaces the entire resource,PATCHupdates only specific fields.
Why is POST not idempotent?
- Multiple
POSTrequests can create multiple resources, unlikePUT.
How to secure REST APIs?
- Use HTTPS, OAuth2, JWT, API keys, rate limiting.
Why do we use 201 Created instead of 200 for POST?
- 201 explicitly tells the client a resource was created, and usually includes Location header with new resource URI.
Whatβs the difference between 401 and 403?
- 401 Unauthorized = authentication missing/invalid.
- 403 Forbidden = authentication is fine, but user lacks permission.
Why is PUT idempotent but POST is not?
- Repeating PUT /users/1 with same data updates the same resource β same result.
- Repeating POST /users may create multiple users β different result.