My API has a route to process a user by an int id passed in the url. I'd like to pass a list of ids so I can make one bulk request to the API rather than several single requests. How can I accept a list of ids?
@app.route('/user/<int:user_id>') # should accept multiple ints
def process_user(user_id):
return str(user_id)
Rather than passing it in the url, pass a form value. Use request.form.getlist
to get a list of values for a key, rather than a single value. You can pass type=int
to make sure all the values are ints.
@app.route('/users/', methods=['POST'])
def get_users():
ids = request.form.getlist('user_ids', type=int)
users = []
for id in ids:
try:
user = whatever_user_method(id)
users.append(user)
except:
continue
returns users
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With