路径参数
使用创建的enum类(ModelName)创建一个带有类型注释的路径参数:
from enum import Enum
from fastapi import FastAPI
class ModelName(str, Enum):
alexnet = "alexnet"
resnet = "resnet"
lenet = "lenet"
app = FastAPI()
@app.get("/model/{model_name}")
async def get_model(model_name: ModelName):
if model_name == ModelName.alexnet:
return {"model_name": model_name, "message": "Deep Learning FTW!"}
if model_name.value == "lenet":
return {"model_name": model_name, "message": "LeCNN all the images"}
return {"model_name": model_name, "message": "Have some residuals"}
if __name__ == "__main__":
import os
command = 'uvicorn main_2:app --reload'
os.system(command)
检查文档,因为路径参数的可用值是指定的,交互式文档可以很好地显示它们:
“查询”参数
当您声明不属于路径参数一部分的其他函数参数时,它们将被自动解释为“查询”参数。
from fastapi import FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10):
return fake_items_db[skip: skip + limit]
if __name__ == "__main__":
import os
command = 'uvicorn main_4:app --reload'
os.system(command)
查询是在?之后的键-值对集合。在URL中,用&字符分隔。例如,在url http://127.0.0.1:8000/items/?skip=0&limit=10
中,
- skip: with a value of 0
- limit: with a value of 10
因为它们是URL的一部分,所以它们是“自然的”字符串。
但是,当您使用Python类型声明它们时(在上面的示例中为int类型),它们将被转换为该类型并根据该类型进行验证。
应用于路径参数的所有相同过程也适用于查询参数:
- 编辑器支持(很明显)
- 数据“解析”
- 数据验证
- 自动文档
由于查询参数不是路径的固定部分,所以它们可以是可选的,并且具有默认值。
可选参数
同样的,你可以声明可选的查询参数,通过设置它们的默认值为None:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str = None):
if q:
return {"item_id": item_id, "q": q}
return {"item_id": item_id}
if __name__ == "__main__":
import os
command = 'uvicorn main_5:app --reload'
os.system(command)
在本例中,函数参数q是可选的,默认情况下为None。
查询参数类型转换
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str = None, short: bool = False):
item = {"item_id": item_id}
if q:
item.update({"q": q})
if not short:
item.update(
{"description": "This is an amazing item that has a long description"}
)
return item
if __name__ == "__main__":
import os
command = 'uvicorn main_5:app --reload'
os.system(command)
在这种情况下,你跳转到:
http://127.0.0.1:8000/items/foo?short=1
或者:
http://127.0.0.1:8000/items/foo?short=True
或者:
http://127.0.0.1:8000/items/foo?short=true
或者:
http://127.0.0.1:8000/items/foo?short=on
或者:
http://127.0.0.1:8000/items/foo?short=yes
或者任何其他大小写变化(大写,大写的第一个字母,等等),您的函数将看到参数short, bool值为True, 否则为False。
多个路径和查询参数
您可以同时声明多个路径参数和查询参数,FastAPI知道哪个是哪个。你不需要以任何特定的顺序来声明它们。它们将通过名称被检测到:
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
user_id: int, item_id: str, q: str = None, short: bool = False
):
item = {"item_id": item_id, "owner_id": user_id}
if q:
item.update({"q": q})
if not short:
item.update(
{"description": "This is an amazing item that has a long description"}
)
return item
if __name__ == "__main__":
import os
command = 'uvicorn main_5:app --reload'
os.system(command)
需要查询参数
当您为非路径参数声明一个默认值时(目前,我们只看到查询参数),那么它就不是必需的。
如果您不想添加特定的值,而只是将其设为可选的,则将缺省值设为None。
但是当你需要一个查询参数时,你可以不声明任何默认值:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
item = {"item_id": item_id, "needy": needy}
return item
if __name__ == "__main__":
import os
command = 'uvicorn main_5:app --reload'
os.system(command)
这里需要的查询参数是必需的str类型的查询参数。
如果你在你的浏览器打开一个URL像:
http://127.0.0.1:8000/items/foo-item
…如果不添加needy参数,您将看到如下错误:
{“detail”:[{“loc”:[“query”,“needy”],“msg”:“field required”,“type”:“value_error.missing”}]}
因为needy是一个必要的参数,你需要在URL中设置它:
http://127.0.0.1:8000/items/foo-item?needy=sooooneedy
…这样就可以运行了:
{“item_id”:“foo-item”,“needy”:“sooooneedy”}
当然,您可以根据需要定义一些参数,有些参数具有默认值,有些则完全是可选的:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str, skip: int = 0, limit: int = None):
item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
return item
在这种情况下,有3个查询参数:
needy, a required str.
skip, an int with a default value of 0.
limit, an optional int.