简体   繁体   English

Flask_restx @api.route return 语句导致 TypeError

[英]Flask_restx @api.route return statement results in a TypeError

I have a problem I am not able to solve.我有一个我无法解决的问题。 I want to make use of http cookies in flask.我想在 flask 中使用 http cookies。 This is the code from documentation:这是文档中的代码:

@app.route('/token/auth', methods=['POST'])
def login():
    username = request.json.get('username', None)
    password = request.json.get('password', None)
    if username != 'test' or password != 'test':
        return jsonify({'login': False}), 401

    # Create the tokens we will be sending back to the user
    access_token = create_access_token(identity=username)
    refresh_token = create_refresh_token(identity=username)

    # Set the JWT cookies in the response
    resp = jsonify({'login': True})
    set_access_cookies(resp, access_token)
    set_refresh_cookies(resp, refresh_token)
    return resp, 200

I use flask_restx which automatically turns the response into JSON, so that jsonify in the first example is not needed.我使用flask_restx ,它会自动将响应转换为JSON,因此不需要第一个示例中的jsonify However, still still need to jsonify it, because i can not use set_access_cookie on a dictionary.但是,仍然需要对其进行 jsonify,因为我不能在字典上使用set_access_cookie This results at the end in a nested response like this jsonify(jsonify(x))这最终导致像这样的嵌套响应jsonify(jsonify(x))

@api.route("/login")
class UserLogin(Resource):
    def post(self):
        """Allows a new user to login with his email and password"""

        email = request.get_json()["email"]
        password = request.get_json()["password"]

        user = User.query.filter_by(email=email).one_or_none()

        if user is None:
            return {"message": "user does not exist"}, 404

        user = user.format()
        if bcrypt.check_password_hash(pw_hash=user["password"], password=password):

            if user["active"]:
                resp = jsonify({"login": True})
                access_token = create_access_token(identity=user)
                refresh_token = create_refresh_token(user)
                set_access_cookies(resp, access_token)
                set_refresh_cookies(resp, refresh_token)
                return resp, 200
                # return (
                #     {"access_token": access_token, "refresh_token": refresh_token},
                #     200,
                # )

            else:
                return {"message": "User not activated"}, 400

        else:
            return {"message": "Wrong credentials"}, 401

This is the error: TypeError: Object of type Response is not JSON serializable这是错误: TypeError: Object of type Response is not JSON serializable

Any ideas how can I overcome this?任何想法我该如何克服这个问题?

Was able to solve it like this:能够像这样解决它:

data = dict(login=True)
resp = make_response(jsonify(**data), 200)
access_token = create_access_token(identity=user)
refresh_token = create_refresh_token(user)
set_access_cookies(resp, access_token)
set_refresh_cookies(resp, refresh_token)
return resp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM