fastapi-master/docs_src/query_params_str_validations/tutorial003_an.py

17 lines
372 B
Python
Raw Normal View History

2024-08-24 12:41:47 +08:00
from typing import Union
from fastapi import FastAPI, Query
from typing_extensions import Annotated
app = FastAPI()
@app.get("/items/")
async def read_items(
q: Annotated[Union[str, None], Query(min_length=3, max_length=50)] = None,
):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results