[FastAPI-13]pydantic请求体接收数据
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
'''
创建继承BaseModel的类,定义模型user
路径函数中定义形参user,类型为User
通过对象user的属性获取字段的值
客户端使用POST请求
使用Body体 JSON传参
'''
class User(BaseModel):
username: str
password: str
age: int
@app.post("/login")
# user = User(username="str",password="str",age=int)
def login(user: User):
return {
"username": user.username,
"password":user.password,
"age":user.age
}