Files
docs-fastapi/docs/zh/docs/tutorial/cookie-params.md
T
The Scheduler a42b1ff04e
Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
Sync fastapi docs from b5ca1324 on 2025-12-07
2025-12-07 21:35:20 +00:00

888 B
Raw Blame History

Cookie 参数

定义 Cookie 参数与定义 QueryPath 参数一样。

首先,导入 Cookie

{* ../../docs_src/cookie_params/tutorial001_an_py310.py hl[3] *}

声明 Cookie 参数的方式与声明 QueryPath 参数相同。

第一个值是默认值,还可以传递所有验证参数或注释参数:

{* ../../docs_src/cookie_params/tutorial001_an_py310.py hl[9] *}

/// note | 技术细节

CookiePathQuery兄弟类,都继承自共用的 Param 类。

注意,从 fastapi 导入的 QueryPathCookie 等对象,实际上是返回特殊类的函数。

///

/// info | 说明

必须使用 Cookie 声明 cookie 参数,否则该参数会被解释为查询参数。

///

小结

使用 Cookie 声明 cookie 参数的方式与 QueryPath 相同。