Cookie-Parameter¶
So wie Query- und Path-Parameter können Sie auch Cookie-Parameter definieren.
Cookie importieren¶
Importieren Sie zuerst Cookie:
from typing import Annotated
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[str | None, Cookie()] = None):
return {"ads_id": ads_id}
🤓 Other versions and variants
from typing import Annotated, Union
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[Union[str, None], Cookie()] = None):
return {"ads_id": ads_id}
from typing import Union
from readyapi import Cookie, ReadyAPI
from typing_extensions import Annotated
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[Union[str, None], Cookie()] = None):
return {"ads_id": ads_id}
Tip
Prefer to use the Annotated version if possible.
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: str | None = Cookie(default=None)):
return {"ads_id": ads_id}
Tip
Prefer to use the Annotated version if possible.
from typing import Union
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Union[str, None] = Cookie(default=None)):
return {"ads_id": ads_id}
Cookie-Parameter deklarieren¶
Dann deklarieren Sie Ihre Cookie-Parameter, auf die gleiche Weise, wie Sie auch Path- und Query-Parameter deklarieren.
Der erste Wert ist der Typ. Sie können Cookie die gehabten Extra Validierungs- und Beschreibungsparameter hinzufügen. Danach können Sie einen Defaultwert vergeben:
from typing import Annotated
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[str | None, Cookie()] = None):
return {"ads_id": ads_id}
🤓 Other versions and variants
from typing import Annotated, Union
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[Union[str, None], Cookie()] = None):
return {"ads_id": ads_id}
from typing import Union
from readyapi import Cookie, ReadyAPI
from typing_extensions import Annotated
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[Union[str, None], Cookie()] = None):
return {"ads_id": ads_id}
Tip
Prefer to use the Annotated version if possible.
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: str | None = Cookie(default=None)):
return {"ads_id": ads_id}
Tip
Prefer to use the Annotated version if possible.
from typing import Union
from readyapi import Cookie, ReadyAPI
app = ReadyAPI()
@app.get("/items/")
async def read_items(ads_id: Union[str, None] = Cookie(default=None)):
return {"ads_id": ads_id}
Technische Details
Cookie ist eine Schwesterklasse von Path und Query. Sie erbt von derselben gemeinsamen Param-Elternklasse.
Aber erinnern Sie sich, dass, wenn Sie Query, Path, Cookie und andere von readyapi importieren, diese tatsächlich Funktionen sind, welche spezielle Klassen zurückgeben.
Info
Um Cookies zu deklarieren, müssen Sie Cookie verwenden, da diese Parameter sonst als Query-Parameter interpretiert werden würden.
Zusammenfassung¶
Deklarieren Sie Cookies mittels Cookie, auf die gleiche Weise wie bei Query und Path.