Djangorestframework-simplejwt: 'str' object has no attribute 'decode'

Created on 14 Apr 2021  路  11Comments  路  Source: jazzband/djangorestframework-simplejwt

image

This my code on urls.py

from django.contrib import admin
from django.urls import path
from rest_framework_simplejwt import views as jwt_views
from core.views import HelloView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/token/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
    path('hello/', HelloView.as_view(), name='hello'),
]

settings.py

...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
]
...
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}
...

views.py

from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated


class HelloView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        content = {'message': 'Hello, World!'}
        return Response(content)
duplicate

Most helpful comment

install djangorestframework-simplejwt==4.6.0 and see if this solves your problem

All 11 comments

What version of PyJWT and django-rest-framework-simplejwt is installed?

What version of PyJWT and django-rest-framework-simplejwt is installed?

This my pip freeze command:

asgiref==3.3.4
astroid==2.5.3
Django==3.2
djangorestframework==3.12.4
djangorestframework-simplejwt==4.4.0
isort==5.8.0
lazy-object-proxy==1.6.0
mccabe==0.6.1
pkg-resources==0.0.0
PyJWT==2.0.1
pylint==2.7.4
pytz==2021.1
sqlparse==0.4.1
toml==0.10.2
typed-ast==1.4.3
typing-extensions==3.7.4.3
wrapt==1.12.1

install djangorestframework-simplejwt==4.6.0 and see if this solves your problem

I'm using djangorestframework-simplejwt==4.6.0 and I had the same issue. Fixed by using pyjwt==2.0.0 instead of the newer version pyjwt==2.0.1

I looked at the source code djangorestframework-simplejwt and pyjwt.The reason for this error is thebackends.TokenBackend.encode, which returns thetoken.decode ("utf-8"). in pyjwt 2.0 and later jwt.encode method returns str directly instead of the previous' bytes'. To avoid this error, we can use the version of pyjwt less than 2.0, or wait for simplejwt to be modified.

Sorry about this. In the middle of finals (and in the middle of midnight). I'm going to see if I can get permission from @auvipy or @jezdez to push out a release which includes #349 that should fix it.

Please give me ETA May 3rd ish to get SimpleJWT 4.7.0 out.

Scratch that. @auvipy I'm not "project lead" but I've had SimpleJWT 4.7.0 laid out for about a month or two; you think it's good to go? If so, just create the release from the GitHub UI (there's a GitHub action that should automate it hopefully)

Sorry for the long wait y'all. v4.7.0 is now released.

Please update this on pip too

@grvrai it's on pip. We only allow Python 3.7+

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GeoffMahugu picture GeoffMahugu  路  5Comments

nikolaik picture nikolaik  路  4Comments

mosi-kha picture mosi-kha  路  6Comments

robd003 picture robd003  路  6Comments

xianfuxing picture xianfuxing  路  4Comments