Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

819
Visualizações
List of query params with Flask request.args

I am trying to pass comma separated query parameters to a Flask endpoint.

An example URI would be:

localhost:3031/someresource#?status=1001,1002,1003

Looking at the return of request.args or request.args.getlist('status') I see that I only get a string.

ipdb> pp request.args
ImmutableMultiDict([('status', '1001,1002,1003')])
ipdb> request.args.getlist('status')
['1001,1002,1003']

I know I can split the string by comma but that is too hacky. What is the appropriate way to handle this in Flask? Or are my query params wrong format?

Solution
Since Flask does not directly support comma separated query params, I put this in in my base controller to support comma-separated or duplicate query params on all endpoints.

request_data = {}
params = request.args.getlist('status') or request.form.getlist('status')
if len(params) == 1 and ',' in params[0]:
    request_data['status'] = comma_separated_params_to_list(params[0])})
else:
    request_data['status'] = params
def comma_separated_params_to_list(param):
    result = []
    for val in param.split(','):
        if val:
            result.append(val)
    return result
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Its not uncommon REST API design to have commas to pass multiple values for the same key - makes it easier for the user. You're parsing GET args anyway, parsing strings is not that much more hacky. You can choose to raise a 400 HTTP error if their string with commas isn't well formatted.

Some other languages (notably PHP) support 'array' syntax, so that is used sometimes:

/request?status[]=1000&status[]=1001&status[]=1002

The flask variant getlist expects multiple keys:

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def status():
    first_status = request.args.get("status")
    statuses = request.args.getlist("status")
    return "First Status: '{}'\nAll Statuses: '{}'".format(first_status, statuses)
❯ curl "http://localhost:5000?status=5&status=7"
First Status: '5'
All Statuses: '['5', '7']'

There's no standard for this, how multiple GET args are parsed/passed depends on which language/framework you're using; flask is built on werkzeug so it allows this style, but you'll have to look it up if you switch away from flask.

over 4 years ago · Santiago Trujillo Relatório

0

This is what you might want here:

request.arg.to_dict(flat=False)

flat is True by default, so by setting it to False, you allow it to return a dict with values inside a list when there's more than one.

According to to_dict documentation:

 to_dict(flat=True)

    Return the contents as regular dict. If flat is True
    the returned dict will only have the first item present, if flat is False
    all values will be returned as lists.
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda