[Solved]-Django-rest-framework – POST Request returns "Method \"GET\" not allowed."

26👍

For anyone coming across this strange phenomenon of a POST request turning into a GET request mysteriously…

I’m using Django + DRF on Heroku. The problem was so simple it made me want to laugh and cry at the same time.

Heroku redirects requests to http://example.com to http://www.example.com, and this redirection involves a GET request. As a result, the POST request is received as a GET request without any of the headers or body that was initially present.

All of that work and frustration just for a missing ‘www’. Hope this helps somebody in the future!

https://stackoverflow.com/a/23688973/8407856

1👍

You might also see this error if you don’t provide a trailing / on the end of your URL when building the request. For example, Django will give a 405 error for this request:

import requests

response = requests.post(
    url='https://myapi.com/api/endpoint',
    headers={'Authorization': f'Token {authentication_token}'},
    data=data,
)

0👍

in my case, I ignore https:// prefix, so Nginx was redirecting backend.foo.com to https://backend.foo.com, and within this, method POST was changing to GET, so I just added https:// prefix and it worked

Leave a comment