Flask function got multiple values for argument

Question:

I have this Python code:

@reports_api.route('/reports/xlsx/organisations/<int:organisation_id>/', methods=['GET'])
@reconnect_to_db
@check_permissions(request, employee_constraints={}, client_user_constraints={}, in_args=True)
def get_organisation_containers_report_xlsx(organisation_id, employee_id):
    if request.method == 'GET':
        recipient = request.args.get('recipient')
        report_str_io = ExcelReportsManager.get_organisation_containers_report(organisation_id, employee_id, recipient == 'up')
    return flask.jsonify(**report_str_io), 200

When i use route to this api, i’ve get error

TypeError: get_organisation_containers_report_xlsx() got multiple values for argument 'organisation_id'

There is the path, that leads to api:

http://localhost:5000/reports/xlsx/organisations/1/?employee_id=2

What I’m doing wrong?

Asked By: Ingwar

||

Answers:

i think that the first argument is path in flask route methods , add path to the function signature as the first argument and check if it fixed that.

Answered By: ddor254

Error was in function args. Need to use request.args.get('employee_id').

And code will be like this:

@reports_api.route('/reports/xlsx/organisations/<int:organisation_id>/', methods=['GET'])
@reconnect_to_db
@check_permissions(request, employee_constraints={}, client_user_constraints={}, in_args=True)
def get_organisation_containers_report_xlsx(organisation_id):
    if request.method == 'GET':
        recipient = request.args.get('recipient')
        report_str_io = ExcelReportsManager.get_organisation_containers_report(organisation_id, request.args.get('employee_id'), recipient == 'up')
    return flask.jsonify(**report_str_io), 200

It works when we sending args in path to api like this:

http://localhost:5000/reports/xlsx/organisations/1/?employee_id=2
Answered By: Ingwar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.