fastApi笔记04-查询参数和字符串校验

额外校验

使用Query可以对查询参数添加校验

python 复制代码
from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

default:默认值

max_length:最大长度

min_length:最小长度

pattern:正则表达式

声明为必须参数

当使用Query声明一个查询参数为必须参数时,只用不声明默认值就行,也就是default不传

python 复制代码
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

使用省略号(...)声明必填参数

python 复制代码
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(default=..., min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

使用Pydantic中的Required代替省略号

python 复制代码
from fastapi import FastAPI, Query
from pydantic import Required

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(default=Required, min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

使用None声明必须参数

python 复制代码
from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=..., min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

声明None是个有效类型,这样将强制客户端传一个值。即使这个值是None

查询参数列表,多个值

使用Query显式地定义查询参数时,还可以声明它去接收一组值,或换句话来说,接收多个值。

例如,要声明一个可在 URL 中出现多次的查询参数

python 复制代码
from typing import List, Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[List[str], None] = Query(default=None)):
    query_items = {"q": q}
    return query_items

http://localhost:8000/items/?q=foo&q=bar

要声明类型为List的查询参数,需要显示使用Query,否则该参数将被当做请求体解析

具有默认值的查询参数列表/多个值

python 复制代码
from typing import List

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: List[str] = Query(default=["foo", "bar"])):
    query_items = {"q": q}
    return query_items

http://localhost:8000/items/

更多元数据

python 复制代码
from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: Union[str, None] = Query(
        default=None,
        alias="item-query",
        title="Query string143242542",
        description="Query string for the items to search in the database that have a good match",
        min_length=3,
        max_length=50,
        pattern="^fixedquery$",
        deprecated=True,
    ),
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
  • alias:参数别名
  • title:不知道啥效果,没试出来
  • description:自动文档上的参数描述
  • deprecated:True时自动文档提示弃用参数
相关推荐
李昊哲小课8 分钟前
Python办公自动化教程 - 第5章 图表创建 - 让数据可视化
python·信息可视化·数据分析·数据可视化·openpyxl
chushiyunen15 分钟前
python pygame实现贪食蛇
开发语言·python·pygame
Dream of maid17 分钟前
Python-基础2(流程控制)
python
Lenyiin2 小时前
《Python 修炼全景指南:一》从环境搭建到第一个程序
开发语言·python
涛声依旧393162 小时前
Python项目实战:学生信息管理系统
开发语言·python·数据挖掘
kcuwu.2 小时前
Python进阶:生成器与协程,高效并发编程的核心实践
windows·python·php
XiaoQiao6669992 小时前
python 简单题目练手【详解版】【1】
开发语言·python
ZC跨境爬虫2 小时前
极验滑动验证码自动化实战:背景提取、缺口定位与Playwright滑动模拟
前端·爬虫·python·自动化
智算菩萨3 小时前
【Python图像处理】2 数字图像基础与Python图像表示
开发语言·图像处理·python
xiaoshuaishuai83 小时前
Git二分法定位Bug
开发语言·python