Skip to content
Discussion options

You must be logged in to vote

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_version

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…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Kludex
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #2915 on February 27, 2023 23:18.