I have an Angular application making a CORS consuming of a rest api (Django Rest Framework). Now I want to GET
all the user from http://127.0.0.1/api/users
. I have already enabled my backend to allow cors request and I can get the data without problems if the django view is not been set any permission restrict (permission_classes = ()
). But I will fail if there is permission restrict IsAdminUser
. I can the user view with permission successfully in the browser if login in as admin. My question is how to let angular use csrftoken
to consume the rest api with permission.
The UserList view:
# List all the users or create a new user
class UserList(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (IsAdminUser,)
The code in angular:
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.withCredentils = true;
and:
.run(function run($http, $cookies){
// For CSRF token compatibility with Django
$http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
}
The error:
Failed to load resource: the server responded with a status of 403 (Forbidden)