Define in details about the types of Rest API ?
#api
Answer
Overview
REST (Representational State Transfer) APIs follow specific principles and can be categorized by their HTTP methods, resource design, and architectural patterns.
HTTP Method Types
| Method | Operation | Description |
|---|---|---|
| GET | Read | Retrieve a resource |
| POST | Create | Submit data to create a new resource |
| PUT | Replace | Replace an entire resource |
| PATCH | Update | Partially update a resource |
| DELETE | Delete | Remove a resource |
| HEAD | Metadata | Like GET but returns only headers |
| OPTIONS | Capabilities | Returns allowed methods (CORS preflight) |
REST API Maturity Levels (Richardson Model)
textLevel 0 — Plain Old XML / JSON over HTTP (one endpoint) Level 1 — Resources (multiple endpoints: /users, /posts) Level 2 — HTTP Verbs (GET, POST, PUT, DELETE properly used) Level 3 — Hypermedia Controls (HATEOAS — responses include links)
Level 0 (One endpoint)
textPOST /api?action=getUser&id=1 POST /api?action=createUser
Level 2 (Proper REST)
textGET /users → Get all users GET /users/1 → Get user 1 POST /users → Create user PUT /users/1 → Replace user 1 PATCH /users/1 → Update part of user 1 DELETE /users/1 → Delete user 1
Level 3 (HATEOAS)
json{ "id": 1, "name": "Alice", "_links": { "self": {"href": "/users/1"}, "orders": {"href": "/users/1/orders"}, "delete": {"href": "/users/1", "method": "DELETE"} } }
API Authentication Types
| Type | Description | Example |
|---|---|---|
| No Auth | Public endpoint | Public news API |
| API Key | Static key in header | text |
| Bearer Token | JWT in Authorization header | text |
| OAuth 2.0 | Delegated access (Google, Facebook) | Social login |
| Basic Auth | Base64 username:password | Internal APIs |
REST API Response Types
dart// 1. Single resource {"id": 1, "name": "Alice"} // 2. Collection [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}] // 3. Paginated { "data": [...], "page": 1, "perPage": 20, "total": 150, "nextPage": "/users?page=2" } // 4. Error response {"error": "NOT_FOUND", "message": "User not found", "statusCode": 404}
REST vs GraphQL vs gRPC
| Feature | REST | GraphQL | gRPC |
|---|---|---|---|
| Protocol | HTTP | HTTP | HTTP/2 |
| Data format | JSON | JSON | Protobuf |
| Flexibility | Fixed endpoints | Query exactly what you need | Contract-based |
| Performance | Medium | Medium | High |
| Best for | Standard CRUD APIs | Complex UI data fetching | Microservices |
REST API Best Practices
text✅ Use nouns for resources (/users, not /getUsers) ✅ Use HTTP methods correctly (GET to read, POST to create) ✅ Return appropriate HTTP status codes ✅ Use plural resource names (/users, /orders) ✅ Version your API (/v1/users, /v2/users) ✅ Use pagination for lists ✅ Use HTTPS always ❌ Never use verbs in endpoints (/getUser, /deletePost)
Standard REST patterns:
(list),textGET /resources(single),textGET /resources/{id}(create),textPOST /resources(replace),textPUT /resources/{id}(update),textPATCH /resources/{id}(delete).textDELETE /resources/{id}