Is there a way to set cookies using flask-restx? I'm using it in combination with flask-jwt-extended and I would like to have an endpoint that sets the jwt-cookie.
Here is what I've tried so far:
@namespace.route("/login")
class Login(Resource):
@namespace.expect(login_model)
def post(self):
data = namespace.payload
username = data.get("username")
password = data.get("password")
user = User.query.filter_by(username=username).first()
if not user or not check_password_hash(user.password, password):
return {"message": "Unauthorized"}, 401
access_token = create_access_token(identity=user, fresh=True)
response = jsonify()
set_access_cookies(response, access_token)
return response, 200
I get the following Error when sending a request to /login:
TypeError: Object of type Response is not JSON serializable
The code works as expected when switching out the response in the return value for a {}, so the problem seems to be that flask-restx doesn't support returning a Response directly.
I figured out what went wrong. When returning a
Responsedirectly, you must not add a status code. The resulting code looks like this: