django-tastypie PATCH gives me a “400 (Bad Request)”

那年仲夏 提交于 2019-12-04 12:37:16

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.

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!