Search code examples
pythonroutesurl-routingflask-restfulurl-parameters

Python3 Flask route variable shows up as the variable name instead of value passed from postman


I have the following python flask api route:

@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 from postman at http://localhost:5555/api/v1/users/5 always ends up with:

{
    "error": "User id not found!"
}

And the python console output shows:

User id: id
User id: id not found!

which means that the route variable ends up in the function as variable name but not value. This happens for both int and string variable types. This is bizarre. What do I miss?


Solution

  • This is due to mistake in the @Authentication.auth_required decorator function when passing the kwargs to the decorated function. It missed one asterisk. So instead of **kwargs it mistakenly passed *kwargs.