Getting 500 Internal server error Flask app

While trying to reach http://localhost:5000/token through Postman (POST request)

POST request details: { “email”: “”, “password”: “xdE56” }

The purpose is to test the login function. An access token should appear in the response body.
I get the following error:

* Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 112-744-944 - - [07/Dec/2021 18:08:40] "POST /token HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 2328, in __call__
    return self.wsgi_app(environ, start_response)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 2314, in wsgi_app
    response = self.handle_exception(e)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/", line 269, in error_router
    return original_handler(e)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 1760, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 35, in reraise
    raise value.with_traceback(tb)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 2311, in wsgi_app
    response = self.full_dispatch_request()
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 1834, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/", line 269, in error_router
    return original_handler(e)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 1737, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 35, in reraise
    raise value.with_traceback(tb)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 1832, in full_dispatch_request
    rv = self.dispatch_request()
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 1818, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/", line 458, in wrapper
    resp = resource(*args, **kwargs)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/", line 573, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/resources/", line 33, in post
    access_token = create_access_token(, fresh=True)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/", line 157, in create_access_token
    return jwt_manager._create_access_token(identity, fresh, expires_delta, user_claims)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/", line 479, in _create_access_token
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/", line 77, in encode_access_token
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/", line 31, in _encode_jwt
AttributeError: 'str' object has no attribute 'decode'

The Postman response body should display the access_token but it doesn’t.

Can that be somewhat related to encoding issues ?

Nowhere in the code can be found something like .decode(‘utf-8’)>.
In Python3, all strings are unicode so the usual solution should be to drop the decode('utf-8') but that’s not the case.
I’m using Flask-JWT-Extended 3.20.0 as a library, which is certainly compatible with Python3

Any suggestions?

The following is the related piece of code
from http import HTTPStatus
from flask import request
from flask_restful import Resource
from flask_jwt_extended import (

  • create_access_token,*
  • jwt_refresh_token_required,*
  • create_refresh_token,*
  • get_jwt_identity,*
  • jwt_required,*
  • get_raw_jwt*

from utils import check_password
from models.user import User

black_list = set()

class TokenResource(Resource):

  • def post(self):*

  •    json_data = request.get_json()*
  •    email = json_data.get('email')*
  •    password = json_data.get('password')*
  •    user = User.get_by_email(email=email)*
  •    if not user or not check_password(password, user.password):*
  •        return {'message': 'username or password is incorrect'}, HTTPStatus.UNAUTHORIZED*
  •    access_token = create_access_token(, fresh=True)*
  •    refresh_token = create_refresh_token(*
  •    return {'access_token': access_token, 'refresh_token': refresh_token}, HTTPStatus.OK*

class RefreshResource(Resource):

  • @jwt_refresh_token_required*

  • def post(self):*

  •    current_user = get_jwt_identity()*
  •    token = create_access_token(identity=current_user, fresh=False)*
  •    return {'token': token}, HTTPStatus.OK*

class RevokeResource(Resource):

  • @jwt_required*

  • def post(self):*

  •    jti = get_raw_jwt()['jti']*
  •    black_list.add(jti)*
  •    return {'message': 'Successfully logged out'}, HTTPStatus.OK*

There’s not enough information and code to debug. The problems starts in your call here (or something related)

and the error

is likely on something to do with the token, which is likely expected to have been encoded in binary as a buffer, hence the decode to a string. Typically at least part of an access token is encoded during generation. has a flask template, so post your code there and get it failing there like on your local machine and it’s likely someone would be able to help (post a link to your project code). Your access token generator is probably just missing a bit of information (no user id? wrong json encoder/decoder?).

Also, you can post errors and code as code blocks (the preformatted text button in the post editor labelled </> or set off the blocks with three backticks before and after) so they are readable (and copyable since indentation is important in python).

@jda983 It is hard to read your code due to the formatting.
The error is on line 31 of your code. What you are attaching .decode('utf-8') to is a string and should not be a string. I would look into why you are getting a string instead of a JSON object to resolve this issue.