FastAPI学习笔记(一)-9.请求体及验证+查询参数及验证+路径参数及验证 混合
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ''' 2 @author:invoker 3 @project:fastapi202108 4 @file: chapter034.py 5 @contact:invoker2021@126.com 6 @descript: 7 @Date:2021/8/6 13:13 8 @version: Python 3.7.8 9 ''' 10 from fastapi import APIRouter, Query, Path 11 from pydantic import BaseModel, Field 12 13 app034 = APIRouter() 14 15 16 # 1.请求体参数 17 class CityInfo(BaseModel): 18 name: str = Field(default='Wuhan', title="城市名称",min_length=5) 19 country: str = Field(..., title='国家名称',min_length=5) 20 country_code: str = Field(default=None, title='国家代码') 21 country_population: int = Field(default=800, title='国家人口数量', ge=800) 22 23 class Config: 24 schema_extra = { 25 "example": { 26 "name": "Wuhan", 27 "country": "China", 28 "country_code": "CN", 29 "country_population": 10000000 30 } 31 } 32 33 34 # 2.Path后面是路径参数 35 # 3.Query后面是查询参数 36 @app034.put("/request_body/city/{name}") 37 def mix_city_info( 38 city01: CityInfo, 39 name: str = Path(default="Wuhan",title="城市名称",min_length=5), 40 confirmed: int = Query(ge=0, description="确诊人数", default=0), 41 death: int = Query(ge=0, description="死亡人数", default=0), 42 ): 43 if name == "Wuhan": 44 return {"Wuhan": {"confirmed": confirmed, "death": death}} 45 return city01.dict()
三种类型的请求及验证
本文来自博客园,作者:kaer_invoker,转载请注明原文链接:https://www.cnblogs.com/invoker2021/p/15108416.html