Mix Path
, Query
and body parameters
我们可以自由的混合 Path
和 Query
和 request body,FastAPI 知道如何自动处理
request body 参数也是可选的
from fastapi import FastAPI, Path
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
@app.put("/items/{item_id}")
async def update_item(
*,
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
q: str = None,
item: Item = None,
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
if item:
results.update({"item": item})
return results
注:再上面的例子中,request body 是可选的
Multiple body parameters
可以同时声明多个 request body,如下面的例子同时声明了 Item
和 User
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
class User(BaseModel):
username: str
full_name: str = None
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item, user: User):
results = {"item_id": item_id, "item": item, "user": user}
return results
注:上述例子中期望的 body 会加上参数名作为字段名称,即
{
"item": {
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
},
"user": {
"username": "dave",
"full_name": "Dave Grohl"
}
}
FastAPI 将会对接收到的 body 进行自动转换和数据验证,因此 item
将会自动被转换为 Item
类型
Singular values in body
对于查询参数和路径参数而言,可以用 Query
和 Path
声明额外的数据。FastAPI 为 request body 提供了 Body
函数
例如,在上面的例子中,想要声明另一个单值键 importance
,如果直接在参数中声明,会由于是单值而被 FastAPI 识别为查询参数,这时可以使用 Body
来告诉 FastAPI 要声名的是 Body
from fastapi import Body, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
class User(BaseModel):
username: str
full_name: str = None
@app.put("/items/{item_id}")
async def update_item(
*, item_id: int, item: Item, user: User, importance: int = Body(...)
):
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
return results
按照上述声明,则结果为
{
"item": {
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
},
"user": {
"username": "dave",
"full_name": "Dave Grohl"
},
"importance": 5
}
Multiple body params and query
单值变量将会被解释为查询参数,而不需要用 Query
显式声明,例如
from fastapi import Body, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
class User(BaseModel):
username: str
full_name: str = None
@app.put("/items/{item_id}")
async def update_item(
*,
item_id: int,
item: Item,
user: User,
importance: int = Body(..., gt=0),
q: str = None
):
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
if q:
results.update({"q": q})
return results
中 q
为查询参数
注:Body
和 Query
,Path
一样有所有的验证参数和元信息
Embed a single body parameter
为 Body
添加 embed
参数可以使得单个 body 也是嵌入的,例如
from fastapi import Body, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item = Body(..., embed=True)):
results = {"item_id": item_id, "item": item}
return results
期望的 body 是
{
"item": {
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
}
}