Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-rest-framework - POST Request returns "Method \"GET\" not allowed."

I have set up django-rest-auth as per the installation tutorial, yet I am not able to use the login API endpoint. When I send a POST request with the correct information, I receive a 405 status error in response with "Method "GET" not allowed."

However, when I navigate to the actual URL and POST it from the online form, it works fine and returns a token.

Help?!

Example with Postman:

https://i.imgur.com/574rERm.png

Example with Axios:

axios.post('http://----.com/api/accounts/login/', data: {'username': ---, 'password': ---})
.then(function (response) {console.log(response);})
.catch(function (response) {console.log(response);})

UPDATE:

This seems to be an issue with possibly Heroku or Gunicorn? The website is deployed on Heroku using Gunicorn, but all POST Requests are being received as GET requests.

like image 442
Priyam Mohanty Avatar asked Jul 14 '18 21:07

Priyam Mohanty


1 Answers

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

like image 136
Priyam Mohanty Avatar answered Oct 26 '22 02:10

Priyam Mohanty