Skip to content

๐ŸŽš ๐Ÿ”ข

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐ŸŽš ๐Ÿ”ข ๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ”ฌ Query, Path & Cookie ๐Ÿ”ข.

๐Ÿฅ‡ ๐Ÿ—„ 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}
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}
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}
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}
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}
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}

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ”— โฎ๏ธ ๐Ÿ‘ˆ โžก ๐Ÿ› ๏ธ ๐Ÿ“จ 2๏ธโƒฃ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐ŸŽš ๐Ÿ’–:

X-Token: foo
X-Token: bar

๐Ÿ“จ ๐Ÿ”œ ๐Ÿ’–:

{
    "X-Token values": [
        "bar",
        "foo"
    ]
}

๐ŸŒƒ

๐Ÿ“ฃ ๐ŸŽš โฎ๏ธ Header, โš™๏ธ ๐ŸŽ โš  โš“ Query, Path & Cookie.

& ๐Ÿšซ ๐Ÿ˜Ÿ ๐Ÿ”ƒ ๐ŸŽฆ ๐Ÿ‘† ๐Ÿ”ข, FastAPI ๐Ÿ”œ โœŠ ๐Ÿ’… ๐Ÿญ ๐Ÿ‘ซ.