Skip to content

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

@D3X

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)

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy