[Fixed]-'str' object has no attribute 'decode' on djangorestframework_simplejwt

25👍

Downgrading PyJWT did the job for me.

To achieve that, change the corresponding line in your requirements.txt to

PyJWT==v1.7.1

or install the specified package with:

pip install pyjwt==v1.7.1
👤btzs

5👍

for me let downgrade jwt to version 1.7.1

to do that update corresponding line requirement.txt
or if not PyJWT line than add this line

PyJWT==v1.7.1
👤ketan

4👍

Remove .decode(‘utf-8’) in "/venv/lib/python3.9/site-packages/rest_framework_jwt/utils.py" like

# /venv/lib/python3.9/site-packages/rest_framework_jwt/utils.py
def jwt_encode_handler(payload):
key = api_settings.JWT_PRIVATE_KEY or jwt_get_secret_key(payload)
return jwt.encode(
    payload,
    key,
    api_settings.JWT_ALGORITHM
)#.decode('utf-8') ==> delete this

jwt2.3.0-utils-encode_handler

As you installed simple-jwt, jwt has been upgraded to version-2.3.0 from 1.7.1(maybe).
and by this,

jwt2.3.0-api_jwt-encode

"-> str:" has been added at the method "encode", and this means "encode" returns "str".

so we don’t need .decode(‘utf-8’) anymore in utils.py in rest_framework_jwt.

and if you run server again you may encounter the problem below.

except jwt.ExpiredSignature:
rest_framework.request.WrappedAttributeError: module ‘jwt’ has no attribute ‘ExpiredSignature’

you can fix this as below,

# /venv/lib/python3.9/site-packages/rest_framework_jwt/authentication.py
try:
    payload = jwt_decode_handler(jwt_value)
except jwt.ExpiredSignatureError: # ExpiredSignature => no more exists
    msg = _('Signature has expired.')
    raise exceptions.AuthenticationFailed(msg)
except jwt.DecodeError:
    msg = _('Error decoding signature.')
    raise exceptions.AuthenticationFailed(msg)

jwt2.3.0-authentication-except

this problem has occured because the jwt-version has been upgraded to 2.3.0, and ExpiredSignature doesn’t exist anymore.

But we recommend you to downgrade jwt version to 1.7.1 as we don’t know all the changes made by upgrade.

jwt2.3.0-init.py

jwt1.7.1-init.py

👤hss

1👍

I had PyJWT-2.3.0 installed and I was not even guessing if this issue could be related to the version.

The above answers helped me to crack this. So I am just writing the same thing with some extra log details.

pip install PyJWT==1.7.1

(venv) ip-192-168-1-36:django_proj rishi$ pip install PyJWT==1.7.1
Collecting PyJWT==1.7.1
  Using cached PyJWT-1.7.1-py2.py3-none-any.whl (18 kB)
Installing collected packages: PyJWT
  Attempting uninstall: PyJWT
    Found existing installation: PyJWT 2.3.0
    Uninstalling PyJWT-2.3.0:
      Successfully uninstalled PyJWT-2.3.0
Successfully installed PyJWT-1.7.1

Finally my problem got resolved. Thanks to the previous answers to help me on this.

👤hygull

0👍

For the updated version of PyJWT (2.6.0), refer to the documentation.

It shows how to encode and decode with the latest version of PyJWT.

jwt.decode(encoded_jwt, "secret", algorithms=["HS256"])

Notice, how you pass in the encoded jwt.

0👍

this fixed mine

pip install decode==0.7.1

Leave a comment