I am developing a RESTful API using Flask-restful
and Flask-jwt
. When I use @jwt_required()
for any endpoint and I send a request containing JSON data, I do not get the error message JSON I specify. If I remove the JSON data, the request is handled properly.
Here is the Code:
@jwt_required()
def put(self, user_id):
user = UserModel.find_by_id(user_id)
if user is None:
return {"message": "User not found!"}, 404
Here is the Postman request:
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"
}
And the server log:
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 -
If I remove the JSON:
PUT /user/2 HTTP/1.1
Host: 127.0.0.1:5000
Content-Type: application/json
Authorization: JWT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cache-Control: no-cache
I get the expected response:
{
"message": "User not found!"
}
Here is the method being called:
@classmethod
def find_by_id(cls, _id):
return cls.query.filter_by(id = _id).first()
Update
I found out that I get No handlers could be found for logger "flask_jwt"
for the 1st request