0

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

POST request details: { "email": "u999@jht.com", "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 http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 112-744-944
127.0.0.1 - - [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/app.py", 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/app.py", 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/__init__.py", 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/app.py", 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/_compat.py", 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/app.py", 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/app.py", 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/__init__.py", 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/app.py", 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/_compat.py", 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/app.py", 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/app.py", 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/__init__.py", line 458, in wrapper
    resp = resource(*args, **kwargs)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/views.py", 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/__init__.py", line 573, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/resources/token_management.py", line 33, in post
    access_token = create_access_token(identity=user.id, fresh=True)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/utils.py", 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/jwt_manager.py", line 479, in _create_access_token
    json_encoder=config.json_encoder
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/tokens.py", line 77, in encode_access_token
    json_encoder=json_encoder)
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/tokens.py", line 31, in _encode_jwt
    json_encoder=json_encoder).decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'

The Postman response body should display the access_token.

Encoding issue by any chance?

terence
  • 45
  • 7
  • You have not posted the offending Python code. The `.decode()` is probably superfluous though. – Kate Dec 07 '21 at 19:02
  • do you think that inspecting the code might be useful? shall I post it? – terence Dec 08 '21 at 16:35
  • just in case...it's available here https://stackoverflow.com/questions/70258682/importerror-cannot-import-name-request-from-flask/70260273#70260273 I asked another question actually related to the same problem..thanks. – terence Dec 08 '21 at 17:51

1 Answers1

1

Including the piece of code in Flask that is handling the route (i.e. doing your processing after the POST) usually makes it easier to debug an issue.

From the error logs, I suspect there's a piece of code somewhere that is doing <string>.decode('utf-8') but in Python3, all strings are unicode so the usual solution is to drop the decode('utf-8'). If this is not your code (e.g. you are using a library), you need to make sure the version of the library you're using is compatible with Python3 e.g. see this stackoverflow response for PyJWT library if that is the library you're using.

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15
  • .decode('utf-8') is not part of my code. Also Flask-JWT-Extended 3.20.0 is what I'm using as a library – terence Dec 07 '21 at 22:18