FastAPI 查询参数
在同一个接口里区分缺失值、空值和零值,实际测试分页、布尔别名及多个参数的转换。
学完这一章,你能够
- 在同一个接口里区分缺失值、空值和零值,实际测试分页、布尔别名及多个参数的转换。
读取 ? 后面的键值对
查询字符串以 ? 开始,多个键值对用 & 分隔。FastAPI 常将不在路径模板中的普通标量参数推断为查询参数。显式 Body、Header、Cookie 等声明会指定其它输入位置,结构化模型也有自身规则。
/samples 将 skip 和 limit 转成整数并切片,响应同时列出采用的参数和结果。实验准备 32 条记录,所以跳过 20 条后仍能看见后续记录。输入 skip=wrong 会得到 422 并标出 query.skip。
默认值、空字符串和零
类型和是否必填要分别读。q: str | None = None 允许省略 q,省略时值为 None,?q= 表示实际传入空字符串。单写 q: str | None 没有默认值仍是必填,本站直接请求会得到 422。查询字符串中的字面文本 null 也只是字符串。
/selection/{record} 同时要求路径 record 与查询 source。skip 省略时为 0,limit 省略时为 None。明确发送 limit=0 会保留整数 0。编写可选条件时用 is not None 判断是否有值,直接 if limit 会忽略 0。
布尔参数与错误
short: bool = False 只在缺省时取 False。实验分别请求 1、True、true、on、yes 得到 True,0、false、off、no 得到 False。空文本和 maybe 不属于此版本可识别的布尔表示,返回 422。不能把其它任意值理解为 False。
测试给定的 q 或 short 变化时,比较 JSON 里是否包含说明字段。值转换后交给业务逻辑,short 为真时省略说明,为假时保留。
组合路径和查询
路由模板列出 station_id 和 record。函数故意把 record 写在 station_id 前面,仍能根据名称匹配。对 /stations/7/logs/B7?q=air&short=yes,station_id 是整数 7,record 是 B7,q 是 air,short 是布尔真。
下图来自本站浏览器实际请求。响应中的 7 与 true 是 JSON 数字和布尔值,q 与 record 仍是字符串。此时说明字段被省略。

分页值与实际返回范围
| 本站请求 | skip | limit | 结果含义 |
|---|---|---|---|
| /samples | 0 | 10 | 使用两个默认值,返回 R00 至 R09。 |
| /samples?skip=20 | 20 | 10 | 覆盖起点,返回 R20 至 R29。 |
| /samples?skip=20&limit=5 | 20 | 5 | 两项均采用输入,返回 R20 至 R24。 |
同一路由的四个声明
| 参数 | 声明 | 是否必填 | 来源和缺省行为 |
|---|---|---|---|
| record | record: str | 必填 | 从路径捕获记录名。 |
| source | source: str | 必填 | 从查询字符串读取。省略返回 422。 |
| skip | skip: int = 0 | 可选 | 省略时采用整数 0。 |
| limit | limit: int | None = None | 可选 | 省略为 None,输入 0 时保留 0。 |
观测记录分页和查询条件
from fastapi import APIRouter
routes = APIRouter()
samples = [f"R{i:02}" for i in range(32)]
@routes.get("/samples")
def samples_page(skip: int = 0, limit: int = 10):
return {"skip": skip, "limit": limit, "items": samples[skip:skip+limit]}
@routes.get("/lookup/{record}")
def lookup(record: str, q: str | None = None):
return {"record": record, "q": q}
@routes.get("/query-required")
def required_nullable(q: str | None):
return {"q": q}
@routes.get("/read/{record}")
def read(record: str, short: bool = False):
return {"record": record, "short": short} | ({} if short else {"description": "观测记录说明"})
@routes.get("/selection/{record}")
def select(record: str, source: str, skip: int = 0, limit: int | None = None):
return {"record": record, "source": source, "skip": skip, "limit": limit}
@routes.get("/stations/{station_id}/logs/{record}")
def log(record: str, station_id: int, q: str | None = None, short: bool = False):
return {"record": record, "station_id": station_id, "q": q, "short": short} | (
{} if short else {"description": "站点日志说明"}
)
- 分页返回指定切片,布尔别名转换为真或假,不识别的布尔值和缺少必填查询项返回 422。
所需环境:CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0
当前源码 SHA256:b1edc63e68d57a78e57f4bc677b47dd6b8d511e8687299e004e30587a86bed4f
完整开发工程
解压后按 README 选择依赖。完整工程包含路由、数据模型、测试、文档资源和启动配置。
所需环境:CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0
ZIP SHA256:3e3decf8a9a260c814a244335586a20e3253443dc9ec5ae2ac0efd58a9d6279b
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
比较 /selection/A3?source=field 与 /selection/A3?source=field&limit=0,再测试 short=maybe。
展开参考解答
第一条 limit 为 null,第二条为数字 0。给声明为 bool 的 short 传 maybe 会出现 query.short 的 422。