Problem
My django application often respond
405 Method Now Allowedeven though it's working api on development environment. But if i restart development server (python manage.py runserver) It works.Environment
- macOS Mojave 10.14
- Python 3.6.4 (Isolated environment via Pipenv)
- Django 2.1.4
- djangorestframework 3.8.2
API Code
settings/urls.py (root url file)
from django.urls import path, include import my_account.urls urlpatterns = [ path('account/', include(my_account.urls, namespace='account_v1')), ]my_account/urls.py
from django.urls import path from .apps import MyAccountConfig from .views import TokenView app_name = MyAccountConfig.name urlpatterns = [ path('token/', TokenView.as_view()), ]my_account/views.py
from rest_framework.views import APIView class TokenView(APIView): def post(self, request): # Some Business-Logic Code passLog
Log when 405 occured
System check identified no issues (0 silenced). December 07, 2018 - 11:22:54 Django version 2.1.4, using settings 'my_server.settings.staging' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C. .env Applied [07/Dec/2018 11:24:30] "OPTIONS /account/token/ HTTP/1.1" 200 0 [07/Dec/2018 11:24:30] "{"email":"[email protected]","password":"hidden_password"}POST /account/token/ HTTP/1.1" 405 66Log when working
System check identified no issues (0 silenced). December 07, 2018 - 11:48:01 Django version 2.1.4, using settings 'my_server.settings.staging' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C. .env Applied [07/Dec/2018 11:48:08] "OPTIONS /account/token/ HTTP/1.1" 200 0 [07/Dec/2018 11:48:09] "POST /account/token/ HTTP/1.1" 200 517In chrome network tab, no difference between request for 200 and 405.
2 Answers
I Have faced this same error for the last two months eventually I Solved at the end. Error: When a user token is become expired we will be getting 401 status code with unauthorized message, when this happens the next API is also getting interrupted it returns 405 Method Not Allowed as an error.
Solution : There was a bug issue in Django version 2.1 you have to upgrade your django version to 2.2.
After that check this error it will not raise.
Comments
HTTP_405_METHOD_NOT_ALLOWED
The status code 405 represents that you are requesting in a method that is not allowed. Based on your code, You are defining Token API to be accessible by post method.
look at this log:
[07/Dec/2018 11:48:09] "POST /account/token/ HTTP/1.1" 200 517 in this you are sending clear POST request which will be ok. but look at the provided log for failed cases:
[07/Dec/2018 11:24:30] "{"email":"[email protected]","password":"hidden_password"}POST /v1/account/token/ HTTP/1.1" 405 66 1- For some reason in your UI, you are sending a request with method {"email":"[email protected]","password":"hidden_password"}POST!!!!.
2- and you are sending request to /v1/account/token/ API endpoint which is not registered in your urls at all (based on what info you gave us)
1 Comment
/v1/account/token/ was my typo mistake. And, in chrome inspection, request was valid and has no difference. (Also, same in packet capture tool)