django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Request.data empty when multipart/form-data POSTed

Open CaffeineFueled opened this issue 10 years ago • 55 comments

Django==1.9.2 djangorestframework==3.3.2

This is essentially a repost of #3814. There is already some research and discussion attached to that issue that I will not rehash, but the author closed out the item once a workaround was found.

Is that workaround the long term solution? Is the answer to never touch the Django Request before DRF has a chance to do its thing? This can be difficult to control in projects with third party middleware.

The issue seems to be that, when POSTing data, Django is exhausting the underlying request stream if the WSGIRequest is accessed prior to initialize_request in the DRF View dispatcher. PUTs seem to be unaffected.

A totally contrived example that demonstrates the issue. First example, presuming no middleware Request meddling...

class TestView(APIView):

    def post(self, request, *args, **kwargs):
        # request.data, .FILES, and .POST are populated
        # request._request.FILES and .POST are empty
        return Response()

    def put(self, request, *args, **kwargs):
        # request.data, .FILES, and .POST are populated
        # request._request.FILES and .POST are empty
        return Response()

Second example, forcibly evaluating the Django request before dispatching.

class TestView(APIView):

    def dispatch(self, request, *args, **kwargs):
        p = request.POST  # Force evaluation of the Django request
        return super().dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        # request.data, .FILES, and .POST are empty
        # request._request.FILES and .POST are populated
        return Response()

    def put(self, request, *args, **kwargs):
        # request.data, .FILES, and .POST are populated
        # request._request.FILES and .POST are empty
        return Response()

Cheers!

CaffeineFueled avatar Feb 18 '16 17:02 CaffeineFueled

+1 here. The workaround for the previously closed issue is not a solution for the issue at hand. The truth is that there very well is a use case for accessing the Django POST QueryDict in middleware before DRF can consume it.

foslock avatar Feb 18 '16 23:02 foslock

As there is for streaming content too which breaks the use of middleware introspecting the request content. Moreover, if a middleware do look at the request content, it means it'll bypasses at least partially the content negotiation. At this point, it's probably a good option not to use DRF views and explicitly feed the serializer if required.

xordoquy avatar Feb 18 '16 23:02 xordoquy

I'd also be interested in learning a bit more in your solution about this issue - in particular how to preserve the compatibility with the current behavior.

xordoquy avatar Feb 19 '16 07:02 xordoquy

The closest I've come to a "solution" is to add an additional check in the _parse method of the DRF Request:

...
        try:
            parsed = parser.parse(stream, media_type, self.parser_context)
            # If Django got to the POST/FILES data before we did, just use theirs.
            if (len(parsed.data) + len(parsed.files)) < 1:
                parsed.data = self._request.POST
                parsed.files = self._request.FILES
        except:
            # If we get an exception during parsing, fill in empty data and
...

I'm not sure how dangerous it is to use Django's POST data directly like this (rather than making a copy). They seem to be essentially read only once parsed.

While this retains the current Django WSGIRequest behavior, it leaves it in an inconsistent state insofar as POSTs may or may not have POST/FILE data depending on whether or not the request was touched prior to DRF Request parsing.

CaffeineFueled avatar Feb 21 '16 11:02 CaffeineFueled

Just hit this on production today... As workaround, I've put a check in our base GenericAPIView instead of custom middleware (for compatibility must keep both override methods)

    def initialize_request(self, request, *args, **kwargs):
       request = super().initialize_request(request, *args, **kwargs)
       if request.path.startswith("/api/3/") and request.method == "POST":
          request.method = request.META.get("HTTP_X_HTTP_METHOD_OVERRIDE", request.POST.get("_method", request.method)).upper()
       return request

rsalmaso avatar Feb 24 '16 17:02 rsalmaso

Also running into this problem. It appears to have been introduced in 3.2 (was not experiencing it while using 3.1.1).

Does anyone know if there is a way to reset the request object to its original state after examination or possibly duplicate the object without exhausting the POST data stream?

It feels like DRF should be capable of using an already-examined WSGIRequest.

alukach avatar Mar 14 '16 14:03 alukach

+1 I have run into a similar issue. I created a new nested_route decorator that acts a lot like list_route and detail_route. I usually redirect to another viewset to handle the nested path. The issue was that the stream was being read too early (by the parent viewset dispatch), so I needed to ensure that the initialize_request function was only called once for a given request.

Working off of @rsalmaso's comment above, I overrode the initialize_request method in the children viewsets with the following.

def initialize_request(self, request, *args, **kwargs):
        if not isinstance(request, Request):
            request = super().initialize_request(request, *args, **kwargs)
        return request

This works fine, but I think having some sort of attribute for either ignoring the initialize_request function within dispatch or having a global check to only run initialize_request iff isinstance(request, rest_framework.request.Request) == False. I'm happy to prepare the PR with tests if that could be acceptable.

dannybrowne86 avatar Mar 22 '16 19:03 dannybrowne86

I have the same issue. For now I have reverted to drf 3.2

jab3z avatar Mar 25 '16 14:03 jab3z

I've spent almost 3 hours searching about why my data wouldn't be parsed correctly. I first thought the issue was on my Angular App code. And I finally found this issue. Damn, almost banged my head on my desk.

The only work around working for me is getting back to 3.2.5. I'm surprised this issue has no real fix ?

LouWii avatar Mar 30 '16 06:03 LouWii

This shows what open source really is.

To get this fixed, the first step is to have someone take time and have a deep analysis of when / why and how the behavior change has been introduced. An initial analysis was performed on #3814 though it requires confirmation that the mentioned change did affect the behavior and then identify how the behavior change has been introduced.

Before that, I won't be able to consider the various workarounds as a solution.

xordoquy avatar Mar 30 '16 06:03 xordoquy

Here's what I've managed to track down.

The change definitely occurred in commit 566812a as part of removing method and content overriding. Prior to that change, in request.py, the _load_data_and_files method called _load_method_and_content_type which called _perform_form_overloading. Within _perform_form_overloading was a bit of code that seeded the DRF Request._data attribute with Request._request.POST (which is the Django WSGIRequest POST).

        # At this point we're committed to parsing the request as form data.
        self._data = self._request.POST
        self._files = self._request.FILES
        self._full_data = self._data.copy()
        self._full_data.update(self._files)

This means that, back in _load_data_and_files, _parse was probably never called for multipart POSTs (at least not in the default configuration) and thus DRF did not attempt to re-read the request stream.

In more recent versions of DRF, _load_data_and_files no longer calls _load_method_and_content_type as that method no longer exists, so now execution is falling through to the call to _parse. Herein lies the problem.

For multipart POSTs, _parse is using a DRF MultiPartParser which is using a Django MultiPartParser which is attempting to re-read the request stream which has previously been exhausted.

So, another potential solution would be to just allow Django to handle the parsing of multipart forms. This could be achieved by replacing the MultiPartParser.parse method with something more along the lines of:

    def parse(self, stream, media_type=None, parser_context=None):
        """
        Parses the incoming bytestream as a multipart encoded form,
        and returns a DataAndFiles object.

        `.data` will be a `QueryDict` containing all the form parameters.
        `.files` will be a `QueryDict` containing all the form files.
        """
        parser_context = parser_context or {}
        request = parser_context['request']
        _request = request._request
        if _request.method == 'POST':
            return DataAndFiles(_request.POST, _request.FILES)
        encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
        meta = request.META.copy()
        meta['CONTENT_TYPE'] = media_type
        upload_handlers = request.upload_handlers

        try:
            parser = DjangoMultiPartParser(meta, stream, upload_handlers, encoding)
            data, files = parser.parse()
            return DataAndFiles(data, files)
        except MultiPartParserError as exc:
            raise ParseError('Multipart form parse error - %s' % six.text_type(exc))

This essentially grabs the WSGIRequest that underlies the DRF Request and allows it to perform the request parsing. What I don't know is, are there any conditions under which Django would use another parser when DRF is expecting a MultiPartParser?

Edit: Updated "fix" code. Forgot that Django really only parses POSTs for multipart forms. Also wanted to note that I'm working on tests and eventually a pull request.

CaffeineFueled avatar Apr 01 '16 03:04 CaffeineFueled

I'm depending on this fix. Are there already plans on when to release it?

totycro avatar Jun 27 '16 09:06 totycro

I experience the same in 3.3.3. Had to roll back to 3.2.4 on Django 1.8.13. A patch for this issue is highly anticipated.

vakorol avatar Jun 29 '16 11:06 vakorol

I met the same issue today. Doing PUT instead of POST is really the solution for now.

bolgradov-modata avatar Jun 29 '16 16:06 bolgradov-modata

I'll put it on the list, but no promises if it'll make the 3.4.0 cut or not. We'll see.

lovelydinosaur avatar Jul 04 '16 15:07 lovelydinosaur

I did some digging on this too as I ran into it as well.

(This is django 1.8) The DjangoMultiPartParser loops through the upload_handlers and called handle_raw_input which should return something, but none of them do.

The MemoryFileUploadHandler just set's activated to True and the TemporaryFileUploadHandler doesn't even implement it.

So maybe the handlers being passed are the wrong one, or rest framework is using the DjangoMultiPartParser wrong.

hvdklauw avatar Jul 15 '16 10:07 hvdklauw

+1 Answering @alukach, I have the same issue but only when updating to 3.3.0. With 3.2.0 works fine (I had to downgrade)

vabada avatar Jul 26 '16 10:07 vabada

Ran in to this issue as well :-( Had to drop back to version 3.2

ggilley avatar Aug 25 '16 18:08 ggilley

Anything I (we as a company) can do to help with this? This is preventing us from upgrading to drf >= 3.3 and thus is also blocking our django upgrade.

hvdklauw avatar Sep 06 '16 14:09 hvdklauw

Well, it's on the list, and being re-raised by folks, so I'd expect to deal with it prior to 3.5, hopefully towards the end of this month.

Anything I (we as a company) can do to help with this?

This, this, this...

https://fund.django-rest-framework.org/topics/funding/

(We're just a few % short of what we need for baseline sustainability right now, getting over the mark will mean I can focus exclusively on the project itself)

lovelydinosaur avatar Sep 06 '16 15:09 lovelydinosaur