单个查询字符串
@app.get('/index/{username}')
def index(username: str, id: int): # id为查询字符串 ?id=5
return {"message": "success", "username": username, "id": id}
可选的查询字符串参数
@app.get('/items/{item_id}')
async def read_item(item_id: str, q: Union[str, None] = None): # 也可以写成q: Optional[str] = None
if q:
return {'item_id': item_id, 'q': q}
return {'item_id': item_id}
多个查询字符串
# http://localhost:8000/items/?q=foo&q=bar
@app.get("/items/")
async def read_items(q: Union[List[str], None] = Query(default=None)):
query_items = {"q": q}
return query_items
具有默认值的查询参数列表 / 多个值
@app.get("/items/")
async def read_items(q: List[str] = Query(default=["foo", "bar"])):
query_items = {"q": q}
return query_items
标签:None,return,FastAPI,查询,item,str,items,字符串,id
From: https://www.cnblogs.com/weiweivip666/p/18041125