Replies: 3 comments
-
@wozniakty
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
So you can just use It goes on to say:
Personally, I more frequently want to handle any kind of list as JSON than as a query parameter, but I think it could make sense to handle lists of primitive types (float, int, str, bool and optional varieties) as query parameters by default. But this would be a breaking change, so unclear whether it's worth it. |
Beta Was this translation helpful? Give feedback.
-
Ok thanks, I apparently missed some of the nuance in the documentation, my bad. From my first read through the documentation, I would say that it feels a bit weird for a string list (fairly common query param type) is assumed to be a body. However, I can see why that might be harder to differentiate from a list containing a more complex object. I'll close this out. Was a bit of a hard to understand nuance but it's obviously not a bug. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Thanks @dmontagu for your help! 🚀 🍰 🌮 Thanks @wozniakty for reporting back and closing the issue. |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
FastAPI is not correctly auto-interpreting the
List
typing as a query parameter, and is instead treating it as a body parameter and failing.To Reproduce
Steps to reproduce the behavior:
Create a file like the following:
Expected behavior
pathurl?c_types=something&c_types=somethingelse
resolves correctly:c_types=["something", "somethingelse"]
Actual behavior
FastAPI treats the list typing as a Body declaration (like a pydantic type would be interpreted) and throws this error:
Environment:
Additional context
This problem isn't immediately blocking. Adding a
= Query()
causes the query parameter to be interpreted correctly. However, this forces you to set a default and therefore isn't ok for required parameters (admittedly, few query params are required, but still).It's also noteworthy that the problem doesn't exist for all kinds of typing syntax. Doing a
Union[int, bool]
is still treated as a query param (and is validated correctly).Beta Was this translation helpful? Give feedback.
All reactions