Redirecting from DELETE to GET in Django

I use Django django 4.0.8 and django-tastypie.

Why is it that when I send a request (for example) DELETE http://127.0.0.1:8000/api/v1/courses/3 via Postman, I get a redirect to GET /api/v1/courses/3/?

If I send http://127.0.0.1:8000/api/v1/courses/3/ it work correctly.

Adding 'APPEND_SLASH = True' in settings.py does not resolve this problem.

String "MIDDLEWARE = ['django.middleware.common.CommonMiddleware']" I have in settings.p

Why is it that when I send a request (for example) http://127.0.0.1:8000/api/v1/courses/3 via Postman, I get a redirect to GET /api/v1/courses/3/?

Because that is how many browsers (and API clients) implemented it [wiki]:

Many web browsers implemented this code in a manner that violated this standard, changing the request type of the new request to GET, regardless of the type employed in the original request (e.g. POST).

Indeed, a redirect response [wiki] (status code between 300 and 399) specifies the URI in the header, but without the method. The original design was to make an identical request [wiki]:

This is an invitation to the user agent (e.g. a web browser) to make a second, otherwise identical, request to the new URL specified in the location field. The end result is a redirection to the new URL.

But this was mostly ignored. Probably to implement a Post/Redirect/Get architectural pattern [wiki].

One can use HTTP 307 Temporary Redirect [wiki] or HTTP 308 Permanent Redirect [wiki] for this. Django works with a HTTP 301 redirect however, as we can see by the documentation on the APPEND_SLASH setting [Django-doc]:

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.

That being said, for APIs appending slashes looks strange. An API is something designed for machines, so one can be more strict about the behavior you expect from the client.

Back to Top