HTTP methods define the action a client wants to perform on a resource. In Django REST Framework (DRF), each HTTP method is typically mapped to a specific operation such as retrieving, creating, updating, or deleting data.
Understanding HTTP methods is essential for building RESTful APIs.
What are HTTP Methods?
HTTP methods are used by clients to communicate with the server.
Common methods include:
- GET – Retrieve data
- POST – Create new data
- PUT – Update an entire resource
- PATCH – Partially update a resource
- DELETE – Remove a resource
GET Method
The GET method is used to retrieve data from the server.
Example
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeeListAPIView(APIView):
def get(self, request):
employees = [
{"id": 1, "name": "John"},
{"id": 2, "name": "Tarun"}
]
return Response(employees)
Request:
GET /api/employees/
Response:
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Tarun"
}
]
Common Use Cases
- List records
- Retrieve details
- Search and filtering
POST Method
The POST method is used to create new resources.
Example
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
class EmployeeCreateAPIView(APIView):
def post(self, request):
name = request.data.get("name")
return Response(
{
"message": f"{name} created successfully"
},
status=status.HTTP_201_CREATED
)
Request:
{
"name": "Tarun Kumar"
}
Response:
{
"message": "Tarun Kumar created successfully"
}
Common Use Cases
- Create employees
- Register users
- Create products
- Submit forms
PUT Method
The PUT method is used to update an entire resource.
When using PUT, all required fields should typically be provided.
Example
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeeUpdateAPIView(APIView):
def put(self, request, employee_id):
return Response({
"message": f"Employee {employee_id} updated"
})
Request:
{
"name": "Tarun Kumar",
"department": "IT"
}
Response:
{
"message": "Employee 1 updated"
}
Common Use Cases
- Update employee details
- Update product information
- Replace existing records
PATCH Method
PATCH is used to partially update a resource.
Only the fields that need to change are sent.
Example
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeePartialUpdateAPIView(APIView):
def patch(self, request, employee_id):
return Response({
"message": f"Employee {employee_id} partially updated"
})
Request:
{
"department": "HR"
}
Response:
{
"message": "Employee 1 partially updated"
}
Common Use Cases
- Change status
- Update profile image
- Modify a single field
DELETE Method
DELETE is used to remove a resource.
Example
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
class EmployeeDeleteAPIView(APIView):
def delete(self, request, employee_id):
return Response(
{
"message": f"Employee {employee_id} deleted"
},
status=status.HTTP_204_NO_CONTENT
)
Request:
DELETE /api/employees/1/
Response:
{
"message": "Employee 1 deleted"
}
Common Use Cases
- Delete users
- Remove products
- Delete records
Handling Multiple Methods in One APIView
A single APIView can handle multiple HTTP methods.
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeeAPIView(APIView):
def get(self, request):
return Response({"message": "Employee List"})
def post(self, request):
return Response({"message": "Employee Created"})
def put(self, request):
return Response({"message": "Employee Updated"})
def delete(self, request):
return Response({"message": "Employee Deleted"})
This approach is commonly used for CRUD operations.
Method Not Allowed Error
If a client sends a request using an unsupported method, DRF automatically returns:
{
"detail": "Method \"POST\" not allowed."
}
Status Code:
405 Method Not Allowed
HTTP Methods and CRUD Mapping
| CRUD Operation | HTTP Method |
|---|---|
| Create | POST |
| Read | GET |
| Update | PUT / PATCH |
| Delete | DELETE |
Best Practices
- Use GET only for retrieving data.
- Use POST for creating new resources.
- Use PUT for complete updates.
- Use PATCH for partial updates.
- Use DELETE for removing resources.
- Return appropriate status codes.
- Validate request data using serializers.
Conclusion
HTTP methods form the foundation of RESTful APIs. Django REST Framework makes it easy to handle GET, POST, PUT, PATCH, and DELETE requests through APIView methods. Using the correct HTTP method improves API consistency, readability, and maintainability.