FastAPI学习笔记(一)-7.查询参数及验证
一、查询参数及验证
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ''' 2 @author:invoker 3 @project:fastapi202108 4 @file: chapter032.py 5 @contact:invoker2021@126.com 6 @descript: 7 @Date:2021/8/6 7:40 8 @version: Python 3.7.8 9 ''' 10 from fastapi import APIRouter, Query 11 from typing import Optional, List 12 13 app032 = APIRouter() 14 15 """ 16 3.2 查询参数及字符串验证 17 """ 18 19 20 # 1.有默认值表示参数为非必填项 21 @app032.get('/query') 22 def page_limit(page: int, limit: Optional[int] = None): 23 if limit: 24 return {"page": page, "limit": limit} 25 return {"page": page} 26 27 28 fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}] 29 print(fake_items_db[0:10]) 30 31 list1 = [1, 2, 3, 4, 5] 32 print(list1[0:10]) 33 34 35 # 说明切片的尾值可以大于List的长度 36 37 38 @app032.get("/items/") 39 async def read_item(skip: int = 0, limit: int = 10): 40 return fake_items_db[skip: skip + limit] 41 42 43 # 2.查询参数为bool时的自动转换 44 # 在url中输入以下,都会转化成True 45 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=yes 46 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=1 47 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=on 48 49 # 在url中输入以下,都会转化成False 50 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=no 51 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=0 52 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=off 53 54 # 在url中输入其他,都会报错 55 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=2 56 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=hello 57 # http://127.0.0.1:8000/chapter032/query/bool/convert?param=offset 58 59 @app032.get("/query/bool/convert") 60 async def type_convert(param: bool = False): 61 return param 62 63 64 # 3.查询参数的验证 65 # 3.1第一个参数没有给default值,所以为必填参数,且必须是a开头,长度7-18位的字符串 66 # 3.2第二个参数给了default值,所以为非必填参数 67 @app032.get('/query/validations') 68 def query_param_validate( 69 value: str = Query(..., min_length=7, max_length=18, regex='^a'), 70 values: List[str] = Query(default=['d1', 'd2'], alias="别名1") 71 ): 72 return value, values
1.1非路径参数,且为普通类型(int,str,bool,float)的均为查询参数
1.2 有默认值的查询参数均为选填项
1.3 没有默认值的查询参数均为必填项
# 1.有默认值表示参数为非必填项
@app032.get('/query')
def page_limit(page: int, limit: Optional[int] = None):
if limit:
return {"page": page, "limit": limit}
return {"page": page}
在线接口文档及执行结果:
1.4 bool类型的查询参数,可以自动转换
# 2.查询参数为bool时的自动转换
# 在url中输入以下,都会转化成True
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=yes
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=1
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=on
# 在url中输入以下,都会转化成False
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=no
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=0
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=off
# 在url中输入其他,都会报错
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=2
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=hello
# http://127.0.0.1:8000/chapter032/query/bool/convert?param=offset
@app032.get("/query/bool/convert")
async def type_convert(param: bool = False):
return param
1.5 查询阐述的验证,使用Query类,default值设置后,则对应的变量参数为选填。
# 3.查询参数的验证
# 3.1第一个参数没有给default值,所以为必填参数,且必须是a开头,长度7-18位的字符串
# 3.2第二个参数给了default值,所以为非必填参数
@app032.get('/query/validations')
def query_param_validate(
value: str = Query(..., min_length=7, max_length=18, regex='^a'),
values: List[str] = Query(default=['d1', 'd2'], alias="别名1")
):
return value, values
在线接口文档及执行结果:
本文来自博客园,作者:kaer_invoker,转载请注明原文链接:https://www.cnblogs.com/invoker2021/p/15106927.html