I wrote a decorator that tries to verify that we have data for the POST route:
Here is my decorator:
def require_post_data(required_fields=None): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): for required_field in required_fields: if not request.form.get(required_field, None): return jsonify({"error": "Missing %s from post data." % required_field}), 400 else: if not request.form: return jsonify({"error": "No post data, aborting."}), 400 return f(*args, **kwargs) return decorated_function return decorator
And I have two routes, with the URL parameter and the other without:
from flask import Blueprint, jsonify, request mod = Blueprint('contacts', __name__, url_prefix='/contacts') @mod.route('/', methods=['POST']) @require_post_data(['customer_id', 'some_other_required_field']) def create_contact():
When I run a test that falls into update_contact
, I get the following exception:
TypeError: decorator() got an unexpected keyword argument 'contact_id'
But it seems that create_contact
working as expected.
Why is contact_id
passed to decorator()
?
source share