question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] List parameters in query string format

See original GitHub issue

Description

Currently, only format with plain parameter name is supported when passing list of parameters https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#query-parameter-list-multiple-values and only q=1&q=2&q=3 format is supported, however passing list parameters using indexed or not indexed square brackets is widely used across the web and web APIs. None of this is, unfortunately a standard defined in any RFC.

There are 3 most common ways of passing lists out there:

  • q=1&q=2&q=3
  • q[]=1&q[]=2&q[]=3
  • q[0]=1&q[1]=2&q[2]=3

Some sdks, or clients for making requests by default send list using square brackets so there are some issues with configuration quite often.

Are there any plans to support square brackets for list parameters along with the current format?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
haynesgtcommented, Oct 20, 2021

This is what I ended up putting in my app:

@app.middleware("http")
async def fix_list_query_params(request: fastapi.Request, call_next):
    # Rails / some libs put square brackets on list parameters. FastAPI does not use the square brackets.
    def fix_key(key: str):
        if key and key.endswith("[]"):
            key = key[:-2]
        return key

    request._query_params = QueryParams(
        [(fix_key(key), value) for key, value in request.query_params.multi_items()]
    )
    request.scope["query_string"] = str(request.query_params)
    response = await call_next(request)
    return response
2reactions
haynesgtcommented, Jun 8, 2022

from fastapi.datastructures import QueryParams or from starlette.datastructures import QueryParams

Read more comments on GitHub >

github_iconTop Results From Across the Web

REST API Design Best Practices for Parameter and Query ...
There are many ways in HTTP to add parameters to our request: the query string, the body of POST, PUT and PATCH requests,...
Read more >
How to pass an array within a query string? - Stack Overflow
Here's what I figured out: Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ...
Read more >
Query string - Wikipedia
A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters. A query string commonly includes...
Read more >
Passing Information via Query Strings - Qualtrics
Let's take a look at an example URL with a query string appended to it. The query string is in bold. https://survey.qualtrics.com/jfe/form/SID=SV_1234?
Read more >
A Beginner's Guide to URL Parameters - SEMrush
URL parameters (known also as “query strings” or “URL query parameters”) are elements inserted in your URLs to help you filter and organize ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found