FastApi-04-请求体-1_字段


何为请求体

顾名思义,请求体就是在请求过程中客户端携带的数据。

请求体不是一定要携带的,而且请求体不能用 ​​GET​​​ 请求发送,通常我们会使用 ​​POST​​​ 请求发送请求体,当然是用 ​​PUT、DELETE、PATCH​​ 方式也可以发送请求体。

举个栗子

根据请求体做相应的响应,如下返回请求体内容。

​FastApi​​​ 的请求体需要为 ​​dict​​ 格式

代码

@app.post('/models/')
async def add_model(model:str):
return model

接口测试

正常情况FastApi-04-请求体-1_字段_02异常情况FastApi-04-请求体-1_数据模型_03

​FastApi​​ 会帮助我们进行类型检测,基本格式校验等

请求体校验

通常,我们在开发的时候,需要用户根据特定的结构体来发起请求,从而防止攻击和过滤用户。

FastApi 的数据模型

在 ​​FastApi​​​ 中,我们可以借助 ​​pydantic​​​ 的 ​​BaseModel​​ 类来实现请求结构体的定义。

代码

from pydantic import BaseModel

class Mds(BaseModel):
name: str
age: int
home: str

@app.post('/models/')
async def add_model(model:Mds):
return model

接口测试

正常情况FastApi-04-请求体-1_数据模型_04异常情况FastApi-04-请求体-1_字段_05对于错误的,缺失的结构体字段,​​​FastApi​​ 都会帮我们检测处理。

可选字段

​FastApi​​ 的可选字段有以下两种场景

  1. 字段有默认值时
  2. 字段类型为​​Optional​​ 时

代码

from pydantic import BaseModel
from typing import Optional

class Mds(BaseModel):
name: str
age: int = 18
home: str
height: Optional[str]

@app.post('/models/')
async def add_model(model:Mds):
return model

接口测试

只携带必选参数FastApi-04-请求体-1_字段_06携带全部参数FastApi-04-请求体-1_数据模型_07携带多余参数(多余参数会被忽略)FastApi-04-请求体-1_数据模型_08缺少必选参数FastApi-04-请求体-1_接口测试_09结论

请求结构体的字段,可以多,不可缺少必选的。

请求体使用

我们在视图函数内部可以直接使用请求体的属性。

代码

from pydantic import BaseModel
from typing import Optional

class Mds(BaseModel):
name: str
age: int = 18
home: str
height: Optional[str]

@app.post('/models/')
async def add_model(Mds:Mds):
ret = {}
if Mds.name:
ret.update({"Name":Mds.name})
if Mds.height:
ret.update({"height":Mds.height})
return ret

接口测试

FastApi-04-请求体-1_字段_10