-
ProblemI decided to implement api versioning using header values: I want to:
# Validation
class ApiVersionInfo(BaseModel):
api_version: int
resource_version: int
# Add dependency to app / router
def set_api_version_info(request: Request, api_ver: ApiVersionInfo = Header(...)):
request.state.api_info = api_info
# Add dependency to endpoint
def get_api_version_info(request: Request):
return request.state.api_infoBut FastAPI does not allow to do this: I've read https://fastapi.tiangolo.com/tutorial/header-params/. Question: what is the best way to validate multiple headers? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
If you specify the Header arguments individually in your dependency, you will get the validation that you want without having to create a pydantic model. In this situation, that is how I would I would go about it. Your dependency would look like this: def set_api_version_info(
request: Request, api_version: int = Header(...), resource_version: int = Header(...)
):
request.state.api_info = api_versionAnd then just use that as a dependency wherever you want it. It will do the validation you want. If you're dead set on using a pydantic model, you need to overwrite the signature of the model to convert them to Headers. A full example below, modeled after @Mause's excellent answer here about pydantic-> form: #1989 (comment) from fastapi import Depends, FastAPI, Header, Request
from pydantic import BaseModel
def as_header(cls):
"""decorator for pydantic model
replaces the Signature of the parameters of the pydantic model with `Header`
"""
cls.__signature__ = cls.__signature__.replace(
parameters=[
arg.replace(
default=Header(...) if arg.default is arg.empty else Header(arg.default)
)
for arg in cls.__signature__.parameters.values()
]
)
return cls
@as_header
class ApiVersionInfo(BaseModel):
api_version: int
resource_version: int
def set_api_version_info(
request: Request, api_ver: ApiVersionInfo = Depends(ApiVersionInfo)
):
request.state.api_info = api_ver.api_version
app = FastAPI(dependencies=[Depends(set_api_version_info)])
@app.get("/")
def get_api_version_info(request: Request):
return request.state.api_info |
Beta Was this translation helpful? Give feedback.
-
@falkben, that's good solution, thanks! |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @falkben ! 👏 🙇 Thanks for reporting back and closing the issue @poul1x 👍
|
Beta Was this translation helpful? Give feedback.
If you specify the Header arguments individually in your dependency, you will get the validation that you want without having to create a pydantic model. In this situation, that is how I would I would go about it.
Your dependency would look like this:
And then just use that as a dependency wherever you want it. It will do the validation you want.
If you're dead set on using a pydantic model, you need to overwrite the signature of the model to convert them to Headers. A full example below, modeled after @Mause's excellent answer he…