-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.py
25 lines (21 loc) · 971 Bytes
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django.http import QueryDict
class HttpXMethodOverride:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if 'HTTP_X_METHOD_OVERRIDE' in request.META.keys():
http_method = request.META['HTTP_X_METHOD_OVERRIDE']
if http_method.lower() == 'put':
request.method = 'PUT'
request.META['REQUEST_METHOD'] = 'PUT'
request.PUT = QueryDict(request.body)
if http_method.lower() == 'patch':
request.method = 'PATCH'
request.META['REQUEST_METHOD'] = 'PATCH'
request.PATCH = QueryDict(request.body)
if http_method.lower() == 'delete':
request.method = 'DELETE'
request.META['REQUEST_METHOD'] = 'DELETE'
request.DELETE = QueryDict(request.body)
response = self.get_response(request)
return response