一. 路径参数(path)、查询参数(Query)、header参数
注:“路径”通常也称为“端点”或“路由”。
http://127.0.0.1:8000/items/foo?short=yes
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str = None, short: bool = False):
pass
item_id是路径参数
q 和 short是查询参数
二.可选的查询参数、有默认值的查询参数、必需的查询参数
注:可选的查询参数、有默认值的查询参数:这两个可以归为非必须的参数
注:有默认值的查询参数 (这个参数也是可有可去除可改)
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str = None):
pass
参数q是可选的查询参数(就是这个参数可有可去除可改),默认值是None.
注:参数q也可以叫做有默认值的查询参数
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
pass
参数needy是str类型的必需查询参数。
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str, skip: int = 0, limit: int = None):
pass
参数skip是int类型的默认参数(这个参数可有可去除可改)。