My app uses django rest_framework and SessionAuthentication. I can login successfully and have session id and csrf token set in cookie. However, POST request still complains CSRF failure.
- Login with rest_framework BasicAuthentication; session id and csrf token are set cookie
- copy and paste csrf token value to Post request header with key "X-CSRFTOKEN" and value from cookie.
- django.middleware.csrf.CsrfViewMiddleware are in Middleware classes in settings.py
I test with Postman and got
{"detail":"CSRF Failed: CSRF token missing or incorrect."}
class ApiLoginView(APIView):
authentication_classes = (BasicAuthentication, )
permission_classes = (IsAuthenticated,)
def post(self, request, *args, **kwargs):
# use django.contrib.auth.login
login(request, request.user)
user = request.user
return Response("login success")
class ApiUserView(APIView):
authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,)
def post(self, request):
return Response("ApiUser Post Success")
Is Postman a correct tool for testing? this seems to be a similar problem in Postman
Any thing I am missing? and what are the options for me to test django_rest_framework.
Sorry it seems to be a common problems but I cannot find work it through after reading related posts.


