I'm trying to pass the token to the backend but I'm having some issues.
when I'm writing this in the cmd:
curl -H "Authorization: Token 3c9c6079602737a04fcb6b2e737804142ef93930e4e705c0598b2fc597759f7f" http://127.0.0.1:8000/api/auth/user/
I get the user like I wanted.
but when I'm sending the request from the postman / frontend, I get this error:
{ "detail": "Authentication credentials were not provided." }
here is my service at the frontend
const config = {headers: {Authorization: `Token ${token}`}} async function query() { try { return await axios.get(`http://127.0.0.1:8000/api/${types.TODO_API}/`, null, config); } catch (err) { throw err; }; };
when I'm sending the req, in the XHR - Networks I see it in query string params like this:
headers: {"Authorization":"Token 50633f123efeb7b3f122e5e3ee4e9206463dfa5b413cacca475ab9ffd743da8f"}
here is my setting.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'corsheaders', 'todo', 'user', 'knox' ] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'knox.auth.TokenAuthentication', ) } REST_KNOX = { 'TOKEN_TTL': timedelta(hours=10000), 'USER_SERIALIZER': 'knox.serializers.UserSerializer', } CORS_ALLOW_HEADERS = [ 'accept', 'accept-encoding', 'authorization', 'content-type', 'dnt', 'origin', 'user-agent', 'x-csrftoken', 'x-requested-with', 'access-control-allow-origin', ] CORS_ALLOW_CREDENTIALS = True
here is the TodoViewSet, which also use the token, but not working unless i send the req + token from the cmd.
class TodoViewSet(viewsets.ModelViewSet): permission_classes = (permissions.IsAuthenticated, ) serializer_class = TodoSerializer def get_queryset(self): return self.request.user.todo.all() def preform_create(self, serializer): serializer.save(owner = self.request.user)
here is the GetUserView
class GetUserView(generics.RetrieveAPIView): permission_classes = (permissions.IsAuthenticated,) serializer_class = UserSerializer def get_object(self): return self.request.user
score:0
its working now when I added default Authorization header in axios:
axios.defaults.headers.common['Authorization'] = _getToken();
the _getToken function just returns the token from localStorage in this format Token ${token}
you can change it to Bearer ${token}
or JWT ${token}
so it will work with your jwt plugin.
score:1
can you upload whats in authorization(postman) and you check if the token type is correct.
Credit To: stackoverflow.com
Related Query
- django rest framework problems with passing the header
- Handle a request header in Django rest framework to get the secret key passed in the header and save it with User table?
- Problems with header fields in django rest framework HMAC module?
- Return the current user with Django Rest Framework
- Handle a request header in Django rest framework to get the secret key passed in the header?
- Django Rest Framework with multiple Viewsets and Routers for the same object
- Django Rest Framework - passing Model data through a function, then posting output in a separate field in the same model
- Django Creating a Custom User with the Django Rest Framework
- How to deploy Angular with SSR and Django Rest Framework separately but on the same server
- Django rest framework : How to download image with this image send directly in the body
- How to sync the upload progress bar with upload on s3 bucket using Django Rest Framework
- how i save post with the current user without viewing all user list in the endpoint in django rest framework
- Problems with Django REST framework filtering?
- Using the PATCH method with Django Rest Framework
- Django Rest Framework with Self-referencing many-to-many through unable to get the through model
- Problems sending input file from Vue.js to Django with Django Rest Framework
- Django Rest Framework XML - Create multiple objects with a different xml name field compared to the Django Model
- How do I pass the context from the top level serializer to the contained serializers with Django REST framework
- How to get the requesting application details from Django Rest Framework with Django oAuth toolkit
- How to use asyncio with django rest api framework just the way jquery promises work so that responses don't need to wait
- How to use Last-Modified header with Django Rest Framework ViewSet?
- Using the slug rather than ID in django rest framework with json api
- How to deal with many2many using the Django REST Framework
- Django Rest Framework - Wrapping the JSON response with the name of the model
- Passing a token in the header of a ListAPIView endpoint from one api to another Django REST
- Problems on through model with Django Rest Framework
- How can I verify that all related fields in a Django REST Framework serializer specify objects with the same owner?
- Django Rest Framework trouble with the creation of users
- In django rest framework serializer, how can I return the data of foreign key table along with main table?
- Hiding the products with quantity 0 from view django rest framework
More Query from same tag
- Django postgres full text search in reverse related models
- How to display an animated icon during python function processing in django?
- Unable to access variable user from template in django 1.10
- 400 Bad Request While Using `django.test.client`
- Elegant Disjunctive Normal Form in Django
- django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
- Represent number as currenct from paragraph
- Django. If boolean true = redirect
- Django - Media upload [Errno 13] Permission denied
- Django - using generic views
- Django count queryset, show object instead of PK
- Django foreign keys between abstract models
- Using multiple model fields to regroup list in Django template
- django app consuming rest api - where to put the code
- django change model form instance