我有以下python flask API路由:
@user_api.route("/<int:id>", methods=["GET"])
@Authentication.auth_required
def get_user(id):
"""
Get a user
"""
print(f"User id: {id}")
user = user_schema.dump(UserModel.get_user(id))
if not user:
print(f"User id: {id} not found!")
return custom_response({"error": f"User {id} not found!"}, 400)
return custom_response(user_schema.dump(user), 200)
GET
的邮递员的结尾总是以:
http://localhost:5555/api/v1/users/5
并且python控制台输出显示:
{
"error": "User id not found!"
}
这意味着route变量最终以变量名而不是值的形式出现在函数中。 User id: id
User id: id not found!
和int
变量类型都会发生这种情况。真奇怪我想念什么?
这是由于将string
传递给修饰函数时@Authentication.auth_required
装饰函数的错误。它错过了一个星号。因此,它错误地传递了kwargs
而不是**kwargs
。