Model参数定义:
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
class User(BaseModel):
username: str
full_name: str | None = None
接受多个 Model 作为 Body 参数的接口定义:
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, user: User):
"""
curl -X 'PUT' \
'http://127.0.0.1:18081/items/12345' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"item": {
"name": "nn",
"description": "dd",
"price": 1,
"tax": 2
},
"user": {
"username": "uu",
"full_name": "ff"
}
}'
"""
results = {"item_id": item_id, "item": item, "user": user}
return results
@app.put("/items2/{item_id}")
async def update_item2(
*,
item_id: int,
item: Item,
user: User,
importance: Annotated[int, Body(gt = 0)], # instruct FastAPI to treat it as another body key using Body
q: str | None = None,
):
"""
declare additional query parameters whenever you need, additional to any body parameters
在任何需要的时候声明额外的查询参数,作为对任何主体参数的补充
curl -X 'PUT' \
'http://127.0.0.1:18081/items2/1234?q=qqqqqq' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"item": {
"name": "nnnnn",
"description": "ddddd",
"price": 0,
"tax": 0
},
"user": {
"username": "uuuuu",
"full_name": "ffffff"
},
"importance": 1
}'
The asterisk (*) in the function definition only enforces keyword-only arguments in Python.
It doesn't change how FastAPI interprets or processes the parameters.
The determination of whether a parameter is a body parameter, query parameter, or path parameter is based on:
The parameter's type annotation
The use of FastAPI's special classes like Query(), Body(), etc.
"""
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
if q:
results.update({"q": q})
return results
@app.put("/items3/{item_id}")
async def update_item3(
item_id: int,
item: Item,
user: User,
importance: Annotated[int, Body(gt = 0)], # instruct FastAPI to treat it as another body key using Body
q: str | None = None,
):
"""
curl -X 'PUT' \
'http://127.0.0.1:18081/items3/123?q=qqqq' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"item": {
"name": "string",
"description": "string",
"price": 0,
"tax": 0
},
"user": {
"username": "string",
"full_name": "string"
},
"importance": 1
}'
"""
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
if q:
results.update({"q": q})
return results
@app.put("/items4/{item_id}")
async def update_item4(item_id: int, my_custom_item: Annotated[Item, Body(embed = True)]):
"""
if you want it to expect a JSON with a key my_custom_item and inside of it the model contents,
as it does when you declare extra body parameters,
you can use the special Body parameter embed
curl -X 'PUT' \
'http://127.0.0.1:18081/items4/1234' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"my_custom_item": {
"name": "string",
"description": "string",
"price": 0,
"tax": 0
}
}'
"""
results = {"item_id": item_id, "item": my_custom_item}
return results
不使用 Body(embed=True)时,将不会出现外层的 JSON Key:
@app.put("/items5/{item_id}")
async def update_item5(item_id: int, my_custom_item: Item):
"""
curl -X 'PUT' \
'http://127.0.0.1:18081/items5/1234' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"name": "string",
"description": "string",
"price": 0,
"tax": 0
}'
"""
results = {"item_id": item_id, "item": my_custom_item}
return results
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。