[Django]-Django-tastypie PATCH gives me a "400 (Bad Request)"

4👍

If you are using the latest version of TastyPie (the one from GitHub repository, since August 5th), you can follow the instructions from the documentation:

Using PUT/DELETE/PATCH In Unsupported Places

Some places, like in certain browsers or hosts, don’t allow the PUT/DELETE/PATCH methods. In these environments, you can simulate those kinds of requests by providing an X-HTTP-Method-Override header. For example, to send a PATCH request over POST, you’d send a request like:

curl --dump-header - -H "Content-Type: application/json" -H "X-HTTP-Method-Override: PATCH" -X POST --data '{"title": "I Visited Grandma Today"}' http://localhost:8000/api/v1/entry/1/

So if your host does not support this method, add X-HTTP-Method-Override header with the name of the method you are trying to perform.

👤Tadeck

0👍

If PATCH isn’t getting past your HTTP server, you could fake it. Use a POST request, and add the header ‘X-HTTP-Method-Override’: ‘PATCH’. This is supported in the master branch of Tastypie at the time of this posting.

If you are using an older version, such as the current stable release 0.9.11, you may need a small patch. Something like this gist will teach Tastypie to use that header.

The relevant piece is here:

    if request_method == 'post' and 'HTTP_X_HTTP_METHOD_OVERRIDE' in request.META:
        request_method = request.META['HTTP_X_HTTP_METHOD_OVERRIDE'].lower()

Leave a comment