Skip to content

List query param type interpreted as body #8145

Answered by dmontagu
wozniakty asked this question in Questions
Discussion options

You must be logged in to vote

@wozniakty
This is discussed in the query parameters and string validation docs:

So, when you need to declare a value as required while using Query, you can use ... as the first argument:

from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query(..., min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

If you hadn't seen that ... before: it is a a special single value, it is part of Python and is called "Ellipsis".

So you can just use Query(...) if you want it to be required with no default and no other keyword arguments. (This is also consisten…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by svlandeg
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
Converted from issue

This discussion was converted from issue #321 on February 28, 2023 12:21.