Skip to content

DRF 3.6.4/3.7.0 breaks reading request.POST if request.body was read #5582

Closed

Description

@D3X

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's HttpRequest._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)

Activity

  1. rpkilby commented on Nov 9, 2017

    @rpkilby
    Contributor

    For context, this was changed in #5262, which superseded #5255. Amusingly enough, the original PR did set _post in addition to _files.

    @D3X - would you be able to reduce this into a failing test case?

  2. D3X commented on Nov 9, 2017

    @D3X
    ContributorAuthor

    @rpkilby Thanks for the prompt response. I'll try to submit a PR later tonight.

    (Misclicked on the mobile version and closed the ticket by mistake)

  3. reopened this on Nov 9, 2017
  4. added this to the milestone on Nov 9, 2017
  5. changed the title [-]DRF 3.6.4/3.7.3 breaks reading request.POST directly[/-] [+]DRF 3.6.4/3.7.0 breaks reading request.POST if request.body was read[/+] on Nov 9, 2017
  6. added a commit that references this issue on Nov 9, 2017
  7. D3X commented on Nov 9, 2017

    @D3X
    ContributorAuthor

    @rpkilby I played with it a bit and it seems it only breaks if request.body was read before DRF did its magic.

    Also have added a failing test PR, as you can see above.

  8. xordoquy commented on Nov 9, 2017

    @xordoquy
    Contributor

    This doesn't look very new to me. Did you had that issue in prior versions ?

  9. D3X commented on Nov 9, 2017

    @D3X
    ContributorAuthor

    @xordoquy No. I bumped into it today, while trying to update my project's dependencies, including updating DRF from 3.6.3 to 3.6.4 (can't go to 3.7 yet). It has worked fine before that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions