头部参数模型¶
如果你有一组相关的**头部参数**,你可以创建一个**Pydantic模型**来声明它们。
这将允许你在**多个地方**重用该模型,并且还可以一次性为所有参数声明验证和元数据。😎
/// 注意
自 FastAPI 版本 0.115.0
起支持此功能。🤓
///
使用 Pydantic 模型声明头部参数¶
在**Pydantic模型**中声明你需要的**头部参数**,然后将参数声明为 Header
:
//// 标签 | Python 3.10+
from typing import Annotated
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
host: str
save_data: bool
if_modified_since: str | None = None
traceparent: str | None = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
return headers
////
//// 标签 | Python 3.9+
from typing import Annotated, Union
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
host: str
save_data: bool
if_modified_since: Union[str, None] = None
traceparent: Union[str, None] = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
return headers
////
//// 标签 | Python 3.8+
from typing import List, Union
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing_extensions import Annotated
app = FastAPI()
class CommonHeaders(BaseModel):
host: str
save_data: bool
if_modified_since: Union[str, None] = None
traceparent: Union[str, None] = None
x_tag: List[str] = []
@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
return headers
////
//// 标签 | Python 3.10+ 非 Annotated
/// 提示
如果可能,建议使用 Annotated
版本。
///
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
host: str
save_data: bool
if_modified_since: str | None = None
traceparent: str | None = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: CommonHeaders = Header()):
return headers
////
//// 标签 | Python 3.9+ 非 Annotated
/// 提示
如果可能,建议使用 Annotated
版本。
///
from typing import Union
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
host: str
save_data: bool
if_modified_since: Union[str, None] = None
traceparent: Union[str, None] = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: CommonHeaders = Header()):
return headers
////
//// 标签 | Python 3.8+ 非 Annotated
/// 提示
如果可能,建议使用 Annotated
版本。
///
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
host: str
save_data: bool
if_modified_since: str | None = None
traceparent: str | None = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: CommonHeaders = Header()):
return headers
////
FastAPI 将从请求的**头部**中提取每个字段的数据,并为你提供定义的 Pydantic 模型。
查看文档¶
你可以在 /docs
的文档 UI 中看到所需的头部:
禁止额外头部¶
在某些特殊用例(可能不是很常见)中,你可能希望**限制**你想要接收的头部。
你可以使用 Pydantic 的模型配置来 forbid
任何 extra
字段:
//// 标签 | Python 3.10+
from typing import Annotated
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
model_config = {"extra": "forbid"}
host: str
save_data: bool
if_modified_since: str | None = None
traceparent: str | None = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
return headers
////
//// 标签 | Python 3.9+
from typing import Annotated, Union
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
model_config = {"extra": "forbid"}
host: str
save_data: bool
if_modified_since: Union[str, None] = None
traceparent: Union[str, None] = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
return headers
////
//// 标签 | Python 3.8+
from typing import List, Union
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing_extensions import Annotated
app = FastAPI()
class CommonHeaders(BaseModel):
model_config = {"extra": "forbid"}
host: str
save_data: bool
if_modified_since: Union[str, None] = None
traceparent: Union[str, None] = None
x_tag: List[str] = []
@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
return headers
////
//// 标签 | Python 3.10+ 非 Annotated
/// 提示
如果可能,建议使用 Annotated
版本。
///
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
model_config = {"extra": "forbid"}
host: str
save_data: bool
if_modified_since: str | None = None
traceparent: str | None = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: CommonHeaders = Header()):
return headers
////
//// 标签 | Python 3.9+ 非 Annotated
/// 提示
如果可能,建议使用 Annotated
版本。
///
from typing import Union
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
model_config = {"extra": "forbid"}
host: str
save_data: bool
if_modified_since: Union[str, None] = None
traceparent: Union[str, None] = None
x_tag: list[str] = []
@app.get("/items/")
async def read_items(headers: CommonHeaders = Header()):
return headers
////
//// 标签 | Python 3.8+ 非 Annotated
/// 提示
如果可能,建议使用 Annotated
版本。
///
from typing import List, Union
from fastapi import FastAPI, Header
from pydantic import BaseModel
app = FastAPI()
class CommonHeaders(BaseModel):
model_config = {"extra": "forbid"}
host: str
save_data: bool
if_modified_since: Union[str, None] = None
traceparent: Union[str, None] = None
x_tag: List[str] = []
@app.get("/items/")
async def read_items(headers: CommonHeaders = Header()):
return headers
////
如果客户端尝试发送一些**额外头部**,他们将收到一个**错误**响应。
例如,如果客户端尝试发送一个值为 plumbus
的 tool
头部,他们将收到一个**错误**响应,告知他们头部参数 tool
是不允许的:
{
"detail": [
{
"type": "extra_forbidden",
"loc": ["header", "tool"],
"msg": "Extra inputs are not permitted",
"input": "plumbus",
}
]
}
总结¶
你可以在 FastAPI 中使用 Pydantic 模型 来声明 头部。😎