DRF 3.6.4/3.7.0 breaks reading request.POST if request.body was read · Issue #5582 · encode/django-rest-framework (original) (raw)
Checklist
- I have verified that that issue exists against the
master
branch of Django REST framework. - I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- This is not a usage question. (Those should be directed to the discussion group instead.)
- This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
- I have reduced the issue to the simplest possible case.
- I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
Steps to reproduce
- Create any DRF view
- Add a middleware or a signal handler that uses
request.POST
Expected behavior
request.POST
works fine
Actual behavior
AttributeError
is raised by Django'sHttpRequest._load_post_or_files
The reason the exception is raised is because Django's WSGIReqeust.POST
checks if hasattr(self, '_post')
(which it doesn't have) and proceeds to _load_post_and_data
, which in turn calls parse_file_upload
which tries to set self.upload_handlers
, but the upload_handler.setter
checks if hasattr(self, '_files')
. At this point, _files
has already been set by DRF (to make closing file handles possible), so it raises an exception.
I think the solution to this is to set request._post
in addition to request._files
to preserve the interface. I am not sure if there's an easy way to set it to the original POST
as it seems DRF only calls _load_data_and_files
, which deserializes the data etc.
edited for formatting (@rpkilby)