Performance: Multiple query parameters
This affects how the server parses and handles incoming URL query strings, impacting request processing speed and response time.
from fastapi import FastAPI, Query from typing import List app = FastAPI() @app.get("/items") async def read_items(q: List[str] = Query([])): # process list of query parameters return {"params": q}
from fastapi import FastAPI app = FastAPI() @app.get("/items") async def read_items(q1: str = None, q2: str = None, q3: str = None, q4: str = None, q5: str = None): # process each parameter individually return {"params": [q1, q2, q3, q4, q5]}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple individual query parameters | N/A (server-side) | N/A | N/A | [X] Bad |
| Single list query parameter for multiples | N/A (server-side) | N/A | N/A | [OK] Good |
name (string) and age (integer) in a FastAPI path operation function?Query(...) marks parameters as required query parameters with type hints.Query(...) to be recognized as query parameters.from fastapi import FastAPI, Query
from typing import List
app = FastAPI()
@app.get('/items/')
async def read_items(q: List[str] = Query([])):
return {"q": q}/items/?q=apple&q=banana?List[str] with Query([]) allows multiple values for the same query key to be collected into a list.q=apple and q=banana, so FastAPI collects both into a list.name and age?:from fastapi import FastAPI
app = FastAPI()
@app.get('/user/')
def get_user(name: str, age: int = None):
return {"name": name, "age": age}Optional[type] or type | None to mark them optional properly.age: int = NoneOptional[int] or int | None for FastAPI to treat it as optional.tags (list of strings), limit (integer, default 10), and search (optional string). Which of the following function signatures correctly implements this?Query() to specify default values and metadata for query parameters.Optional[str] or str | None with Query(None) for optional string parameters.Query([]) for list default, Query(10) for limit default, and Optional[str] = Query(None) for optional search, which is correct.Pass the test and this pattern moves unknown