question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jwt.encode generating bytes instead of a string

See original GitHub issue

The documentation example seems to indicate the result of jwt.encode is a string. The actual result with Python 3.7.1 is bytes. If this is just a documentation issue i’m happy to submit a small PR.

Expected Result

>>> import jwt
>>> encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg'

>>> jwt.decode(encoded, 'secret', algorithms=['HS256'])
{'some': 'payload'}

Actual Result

Python 3.7.1 (default, Dec 14 2018, 13:28:58)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import jwt

In [2]: encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')

In [3]: encoded
Out[3]: b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzb21lIjoicGF5bG9hZCJ9.Joh1R2dYzkRvDkqv3sygm5YyK8Gi4ShZqbhK2gxcs2U'

System Information

{
  "cryptography": {
    "version": "2.4.2"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.7.1"
  },
  "platform": {
    "release": "18.2.0",
    "system": "Darwin"
  },
  "pyjwt": {
    "version": "1.7.1"
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:21
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
Samarpitrcommented, May 24, 2019

Thanks @brandonmbanks .decode('utf-8') is working!

7reactions
paulocoutinhoxcommented, Jun 19, 2019

I have the same problem -,-

I solved it with:

encoded_jwt.decode('UTF-8')

This is the correct way for pyjwt?

Read more comments on GitHub >

github_iconTop Results From Across the Web

JWT is being converted from str to bytes on requests.post()
I am writing a Flask application in which I have a service that generates a JWT and passes this onto another service ...
Read more >
Changelog — PyJWT 2.6.0 documentation
Tokens are returned as string instead of a byte string ... And the old v1.x syntax jwt.decode(token, verify=False) is now: jwt.decode(jwt=token, ...
Read more >
Understanding JWT for apps - Atlassian Developer
The claims in a JWT are encoded as a JavaScript Object Notation (JSON) object that is used as the payload of a JSON...
Read more >
How to Handle JWTs in Python - Auth0
Learn how to create, encode, parse, decode and verify your JWTs in Python using ... how to create JWTs in Python using the...
Read more >
OAuth2 with Password (and hashing), Bearer with JWT tokens
We need to install python-jose to generate and verify the JWT tokens in Python ... case) into a sequence of bytes (just a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found