Skip to content

Empty requests treated differently from '{}' #3647

@sorenisanerd

Description

@sorenisanerd

This took me quite a while to work out :)

I have a Serializer with HyperlinkedRelatedField(many=True) on it. I had some API tests that would pass in an empty dict:

self.client.post(list_url, {}, format='json')

The response yielded the expected 401, stating that the given field was required. So far, so good.

Much to my surprise, if I sent an empty POST request to the same url on my production system (or Django's built-in development server), the resource was created succesfully, with an empty list for that field.

After quite a bit of digging, I learned that an empty request invariably means that request.data is a QueryDict, rather than a simple, empty dict. During validation, many Field implementations call html.is_html_dictionary() on request.data. is_html_dictionary simply checks for a .getlist() method. Incidentally, this getlist() method is the culprit. When called, it will always return a list, even if the given key is not found at all, in which case it yields an empty list.

I feel that the result for an empty request and a request that passes in an empty json object should yield the exact same results.

Metadata

Metadata

Assignees

No one assigned

    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