request.body
is defined as a property
in HttpRequest
class.
This is code how body
property looks like:
@property
def body(self):
if not hasattr(self, '_body'):
if self._read_started:
raise RawPostDataException("You cannot access body after reading from request's data stream")
try:
self._body = self.read()
except IOError as e:
six.reraise(UnreadablePostError, UnreadablePostError(*e.args), sys.exc_info()[2])
self._stream = BytesIO(self._body)
return self._body
The aproach that I will use there is to modify _body
attribute in the process_request
method. The return value here is None
, because I want that Django continue processing that request through middleware until to the appropriate view.
class MidddlewareWithHttpPutRequest:
def process_request(self, request):
data = getattr(request, '_body', request.body)
request._body = data + '&dummy_param=1'
# if you call request.body here you will see that new parameter is added
return None