我正在使用Flask-restful
and开发一个 RESTful API Flask-jwt
。当我使用@jwt_required()
任何端点并发送包含 JSON 数据的请求时,我没有收到我指定的错误消息 JSON。如果我删除 JSON 数据,则请求会得到正确处理。
这是代码:
@jwt_required()
def put(self, user_id):
user = UserModel.find_by_id(user_id)
if user is None:
return {"message": "User not found!"}, 404
这是邮递员的请求:
PUT /user/2 HTTP/1.1
Host: 127.0.0.1:5000
Content-Type: application/json
Authorization: JWT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cache-Control: no-cache
{
"backdrop_image": "Sora"
}
和服务器日志:
2017-10-25 18:31:06,256 INFO sqlalchemy.engine.base.Engine (2, 1, 0)
2017-10-25 18:31:06,259 INFO sqlalchemy.engine.base.Engine ROLLBACK
127.0.0.1 - - [25/Oct/2017 18:31:06] "PUT /user/2 HTTP/1.1" 404 -
如果我删除 JSON:
PUT /user/2 HTTP/1.1
Host: 127.0.0.1:5000
Content-Type: application/json
Authorization: JWT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cache-Control: no-cache
我得到了预期的回应:
{
"message": "User not found!"
}
这是被调用的方法:
@classmethod
def find_by_id(cls, _id):
return cls.query.filter_by(id = _id).first()
更新
我发现我得到No handlers could be found for logger "flask_jwt"
了第一个请求