When I do a Http DELETE request to a django app. it directly returns a 301(Moved permanantly) response and redirects me to GET request of same url. I am guessing that Http P
I ran into this issue while using AngularJs and Django REST.
Django Rest was redirecting the client in order to request the URL with a trailing slash while AngularJS' $resource
ensures there is not a trailing dash on its URLs. This is described in this issue
On the same link you will see suggestions to use the more flexible $http
service instead of $resource
. But... I really wanted to use $resource
(less code).
So the solution was trivial, since the Django REST team have accounted for this recently:
router = DefaultRouter(trailing_slash=False)
http://django-rest-framework.org/api-guide/routers.html
(I've added to this to my blog since I'd like to keep a log of a few gotchas using AngularJS & Django REST)
The redirect is caused by the APPEND_SLASH setting:
APPEND_SLASH
Default: True
When set to True, if the request URL does not match any of the patterns in the URLconf and it doesn't end in a slash, an HTTP redirect is issued to the same URL with a slash appended. Note that the redirect may cause any data submitted in a POST request to be lost.
Make sure your server and client are consistent with trailing slashes.
If your project does not need the trailing slashes at all (i.e., it is an api without trailing slashes) you can disable it. Otherwise, make sure your urlconf accept paths without a trailing slash.