3

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

Valeriy
  • 1,365
  • 3
  • 18
  • 45
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47

1 Answers1

0

So I ran this in different possible combination and the issue cannot be replicated. I user below server file

from flask import Flask
from flask_jwt import JWT, jwt_required, current_identity
from flask_restful import Api, Resource
from werkzeug.security import safe_str_cmp

class User(object):
    def __init__(self, id, username, password):
        self.id = id
        self.username = username
        self.password = password

    def __str__(self):
        return "User(id='%s')" % self.id

users = [
    User(1, 'xyz', 'xyz'),
    User(2, 'user2', 'abcxyz'),
]

username_table = {u.username: u for u in users}
userid_table = {u.id: u for u in users}

def authenticate(username, password):
    user = username_table.get(username, None)
    if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
        return user

def identity(payload):
    user_id = payload['identity']
    return userid_table.get(user_id, None)

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'super-secret'

jwt = JWT(app, authenticate, identity)

api = Api(app)

class HelloWorld(Resource):
    @jwt_required()
    def get(self, user_id):
        return {'hello': 'world'}

    @jwt_required()
    def put(self, user_id):
        user = None
        if user is None:
            return {"message": "User not found!"}, 404

api.add_resource(HelloWorld, '/<string:user_id>')


if __name__ == '__main__':
    app.run()

requirements.txt

aniso8601==1.3.0
click==6.7
Flask==0.12.2
Flask-JWT==0.3.2
Flask-RESTful==0.3.6
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
PyJWT==1.4.2
python-dateutil==2.6.1
pytz==2017.2
six==1.11.0
Werkzeug==0.12.2

And then in POSTMAN all requests working whether JSON is sent or not

Without Body Without Body

With Body With Body

It may have to do with some package version issue. I would suggest you run

pip install flask-jwt flask-restful --force --upgrade

To get the latest versions and see if that fixes the issue. If not then try running with debug=True in your app.run()

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • I really appreciate your support. I tried upgrading to latest version and added `debug=True`, I still get `No handlers could be found for logger "flask_jwt"` and I still get `Could not get any response` in Postman although server log is correct `127.0.0.1 - - [03/Nov/2017 22:18:09] "PUT /customer/2 HTTP/1.1" 404 -` – Mohamed_AbdAllah Nov 03 '17 at 20:18
  • @Mohamed_AbdAllah, can't debug a issue without a reproducible source code. – Tarun Lalwani Nov 04 '17 at 04:28