🎚 🔢¶
👆 💪 🔬 🎚 🔢 🎏 🌌 👆 🔬 Query
, Path
& Cookie
🔢.
🗄 Header
¶
🥇 🗄 Header
:
from typing import Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Union[str, None] = Header(default=None)):
return {"User-Agent": user_agent}
🤓 Other versions and variants
from typing import Annotated
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Annotated[str | None, Header()] = None):
return {"User-Agent": user_agent}
from typing import Annotated, Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Annotated[Union[str, None], Header()] = None):
return {"User-Agent": user_agent}
from typing import Union
from fastapi import FastAPI, Header
from typing_extensions import Annotated
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Annotated[Union[str, None], Header()] = None):
return {"User-Agent": user_agent}
Tip
Prefer to use the Annotated
version if possible.
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: str | None = Header(default=None)):
return {"User-Agent": user_agent}
📣 Header
🔢¶
⤴️ 📣 🎚 🔢 ⚙️ 🎏 📊 ⏮️ Path
, Query
& Cookie
.
🥇 💲 🔢 💲, 👆 💪 🚶♀️ 🌐 ➕ 🔬 ⚖️ ✍ 🔢:
from typing import Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Union[str, None] = Header(default=None)):
return {"User-Agent": user_agent}
🤓 Other versions and variants
from typing import Annotated
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Annotated[str | None, Header()] = None):
return {"User-Agent": user_agent}
from typing import Annotated, Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Annotated[Union[str, None], Header()] = None):
return {"User-Agent": user_agent}
from typing import Union
from fastapi import FastAPI, Header
from typing_extensions import Annotated
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Annotated[Union[str, None], Header()] = None):
return {"User-Agent": user_agent}
Tip
Prefer to use the Annotated
version if possible.
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: str | None = Header(default=None)):
return {"User-Agent": user_agent}
📡 ℹ
Header
"👭" 🎓 Path
, Query
& Cookie
. ⚫️ 😖 ⚪️➡️ 🎏 ⚠ Param
🎓.
✋️ 💭 👈 🕐❔ 👆 🗄 Query
, Path
, Header
, & 🎏 ⚪️➡️ fastapi
, 👈 🤙 🔢 👈 📨 🎁 🎓.
Info
📣 🎚, 👆 💪 ⚙️ Header
, ↩️ ⏪ 🔢 🔜 🔬 🔢 🔢.
🏧 🛠️¶
Header
✔️ 🐥 ➕ 🛠️ 🔛 🔝 ⚫️❔ Path
, Query
& Cookie
🚚.
🌅 🐩 🎚 🎏 "🔠" 🦹, 💭 "➖ 🔣" (-
).
✋️ 🔢 💖 user-agent
❌ 🐍.
, 🔢, Header
🔜 🗜 🔢 📛 🦹 ⚪️➡️ 🎦 (_
) 🔠 (-
) ⚗ & 📄 🎚.
, 🇺🇸🔍 🎚 💼-😛,, 👆 💪 📣 👫 ⏮️ 🐩 🐍 👗 (💭 "🔡").
, 👆 💪 ⚙️ user_agent
👆 🛎 🔜 🐍 📟, ↩️ 💆♂ 🎯 🥇 🔤 User_Agent
⚖️ 🕳 🎏.
🚥 🤔 👆 💪 ❎ 🏧 🛠️ 🎦 🔠, ⚒ 🔢 convert_underscores
Header
False
:
from typing import Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(
strange_header: Union[str, None] = Header(default=None, convert_underscores=False),
):
return {"strange_header": strange_header}
🤓 Other versions and variants
from typing import Annotated
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(
strange_header: Annotated[str | None, Header(convert_underscores=False)] = None,
):
return {"strange_header": strange_header}
from typing import Annotated, Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(
strange_header: Annotated[
Union[str, None], Header(convert_underscores=False)
] = None,
):
return {"strange_header": strange_header}
from typing import Union
from fastapi import FastAPI, Header
from typing_extensions import Annotated
app = FastAPI()
@app.get("/items/")
async def read_items(
strange_header: Annotated[
Union[str, None], Header(convert_underscores=False)
] = None,
):
return {"strange_header": strange_header}
Tip
Prefer to use the Annotated
version if possible.
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(
strange_header: str | None = Header(default=None, convert_underscores=False),
):
return {"strange_header": strange_header}
Warning
⏭ ⚒ convert_underscores
False
, 🐻 🤯 👈 🇺🇸🔍 🗳 & 💽 / ⚙️ 🎚 ⏮️ 🎦.
❎ 🎚¶
⚫️ 💪 📨 ❎ 🎚. 👈 ⛓, 🎏 🎚 ⏮️ 💗 💲.
👆 💪 🔬 👈 💼 ⚙️ 📇 🆎 📄.
👆 🔜 📨 🌐 💲 ⚪️➡️ ❎ 🎚 🐍 list
.
🖼, 📣 🎚 X-Token
👈 💪 😑 🌅 🌘 🕐, 👆 💪 ✍:
from typing import List, Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(x_token: Union[List[str], None] = Header(default=None)):
return {"X-Token values": x_token}
🤓 Other versions and variants
from typing import Annotated
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(x_token: Annotated[list[str] | None, Header()] = None):
return {"X-Token values": x_token}
from typing import Annotated, List, Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(x_token: Annotated[Union[List[str], None], Header()] = None):
return {"X-Token values": x_token}
from typing import List, Union
from fastapi import FastAPI, Header
from typing_extensions import Annotated
app = FastAPI()
@app.get("/items/")
async def read_items(x_token: Annotated[Union[List[str], None], Header()] = None):
return {"X-Token values": x_token}
Tip
Prefer to use the Annotated
version if possible.
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(x_token: list[str] | None = Header(default=None)):
return {"X-Token values": x_token}
Tip
Prefer to use the Annotated
version if possible.
from typing import Union
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(x_token: Union[list[str], None] = Header(default=None)):
return {"X-Token values": x_token}
🚥 👆 🔗 ⏮️ 👈 ➡ 🛠️ 📨 2️⃣ 🇺🇸🔍 🎚 💖:
X-Token: foo
X-Token: bar
📨 🔜 💖:
{
"X-Token values": [
"bar",
"foo"
]
}
🌃¶
📣 🎚 ⏮️ Header
, ⚙️ 🎏 ⚠ ⚓ Query
, Path
& Cookie
.
& 🚫 😟 🔃 🎦 👆 🔢, FastAPI 🔜 ✊ 💅 🏭 👫.