I have written a decorator that attempts to check we have post data for a Flask POST route:
Here's 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}), 400else:if not request.form:return jsonify({"error": "No post data, aborting."}), 400return f(*args, **kwargs)return decorated_functionreturn decorator
And I have two routes, with with a URL param and the other without:
from flask import Blueprint, jsonify, requestmod = Blueprint('contacts', __name__, url_prefix='/contacts')@mod.route('/', methods=['POST'])
@require_post_data(['customer_id', 'some_other_required_field'])
def create_contact():# Do some business@mod.route('/<int:contact_id>', methods=['POST'])
@require_post_data
def update_contact(contact_id):# Do some business
When I run a test that hits update_contact
, I'm getting the following exception:
TypeError: decorator() got an unexpected keyword argument 'contact_id'
But it looks like create_contact
is working as expected.
Why is contact_id
being passed into decorator()
?