Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'str' object has no attribute 'decode' python error

Tags:

python

this is my code

@app.route("/api/v1.0/login", methods=["GET"] )
def login():
    auth = request.authorization
    if auth:
        user = users.find_one( { "username" : auth.username } )
        if user is not None:
            if bcrypt.checkpw(bytes(auth.password, 'UTF-8'), user["password"]):
                token = jwt.encode({
                    'user' : auth.username,
                    'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'])
                return make_response( jsonify({'token' : token.decode('UTF-8')}), 200)
            else: 
                return make_response(jsonify({"message" : "Bad password"} ) )
        else: 
            return make_response(jsonify({"message" : "Bad username" } ) )

    return make_response(jsonify({ "message" : "Authentication required"}))

and this is error

AttributeError: 'str' object has no attribute 'decode'

like image 560
Kevin Bradley Avatar asked Apr 17 '26 16:04

Kevin Bradley


1 Answers

If you are using PyJwt module, then there is no need to decode the token. jwt.encode({some_dict}) returns the token you need.

like image 50
therealak12 Avatar answered Apr 19 '26 07:04

therealak12



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!