你可以用定义Query和Path相同的方式来定义Cookie参数。
导入Cookie
首先导入Cookie:
from fastapi import Cookie, FastAPI
声明Cookie参数
声明Cookie参数和Query和Path相同有相同的结构。
第一个值是默认值,您可以传递所有其他验证或注释参数:
from fastapi import Cookie, FastAPIapp = FastAPI()@app.get("/items/")async def read_items(*, ads_id: str = Cookie(None)):return {"ads_id": ads_id}
详细描述
Cookie是Path和查询的“姐妹”类。它还继承自相同的常见Param类。 但是请记住,当您从fastapi导入Query,Path,Cookie等时,这些实际上是返回特殊类的函数。 信息 要声明Cookie,您需要使用Cookie,因为否则参数将被解释为查询参数。
概括
用Cookie声明Cookie, 使用与查询和路径相同的通用模式。
