10
votes

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.

1
How are you sending the request that is failing?Burhan Khalid
@Burhan Khalid I've sent the request using Postman and using Axios (from React Native. Both return status code 405.Priyam Mohanty
The problem is how you are sending the request with Postman / Axios, and not with DRF because clearly its working (as you said yourself) if you use the browser. So unless you edit the question and add the code example that's not working, not sure how to help you here.Burhan Khalid
Hi Burhan, I have added my Axios request code above. Thank youPriyam Mohanty

1 Answers

21
votes

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