๐ ๐ข¶
๐ ๐ช ๐ฌ ๐ ๐ข ๐ ๐ ๐ ๐ฌ 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}
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 ๐ โ ๐ ๐ญ ๐ซ.