Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Join theFastAPI Cloud waiting list 🚀
Follow@fastapi onX (Twitter) to stay updated
FollowFastAPI onLinkedIn to stay updated
Subscribe to theFastAPI and friends newsletter 🎉
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor

Header Parameters

You can define Header parameters the same way you defineQuery,Path andCookie parameters.

ImportHeader

First importHeader:

fromtypingimportAnnotatedfromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(user_agent:Annotated[str|None,Header()]=None):return{"User-Agent":user_agent}
🤓 Other versions and variants

Tip

Prefer to use theAnnotated version if possible.

fromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(user_agent:str|None=Header(default=None)):return{"User-Agent":user_agent}

DeclareHeader parameters

Then declare the header parameters using the same structure as withPath,Query andCookie.

You can define the default value as well as all the extra validation or annotation parameters:

fromtypingimportAnnotatedfromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(user_agent:Annotated[str|None,Header()]=None):return{"User-Agent":user_agent}
🤓 Other versions and variants

Tip

Prefer to use theAnnotated version if possible.

fromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(user_agent:str|None=Header(default=None)):return{"User-Agent":user_agent}

Technical Details

Header is a "sister" class ofPath,Query andCookie. It also inherits from the same commonParam class.

But remember that when you importQuery,Path,Header, and others fromfastapi, those are actually functions that return special classes.

Info

To declare headers, you need to useHeader, because otherwise the parameters would be interpreted as query parameters.

Automatic conversion

Header has a little extra functionality on top of whatPath,Query andCookie provide.

Most of the standard headers are separated by a "hyphen" character, also known as the "minus symbol" (-).

But a variable likeuser-agent is invalid in Python.

So, by default,Header will convert the parameter names characters from underscore (_) to hyphen (-) to extract and document the headers.

Also, HTTP headers are case-insensitive, so, you can declare them with standard Python style (also known as "snake_case").

So, you can useuser_agent as you normally would in Python code, instead of needing to capitalize the first letters asUser_Agent or something similar.

If for some reason you need to disable automatic conversion of underscores to hyphens, set the parameterconvert_underscores ofHeader toFalse:

fromtypingimportAnnotatedfromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(strange_header:Annotated[str|None,Header(convert_underscores=False)]=None,):return{"strange_header":strange_header}
🤓 Other versions and variants

Tip

Prefer to use theAnnotated version if possible.

fromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(strange_header:str|None=Header(default=None,convert_underscores=False),):return{"strange_header":strange_header}

Warning

Before settingconvert_underscores toFalse, bear in mind that some HTTP proxies and servers disallow the usage of headers with underscores.

Duplicate headers

It is possible to receive duplicate headers. That means, the same header with multiple values.

You can define those cases using a list in the type declaration.

You will receive all the values from the duplicate header as a Pythonlist.

For example, to declare a header ofX-Token that can appear more than once, you can write:

fromtypingimportAnnotatedfromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(x_token:Annotated[list[str]|None,Header()]=None):return{"X-Token values":x_token}
🤓 Other versions and variants

Tip

Prefer to use theAnnotated version if possible.

fromfastapiimportFastAPI,Headerapp=FastAPI()@app.get("/items/")asyncdefread_items(x_token:list[str]|None=Header(default=None)):return{"X-Token values":x_token}

If you communicate with thatpath operation sending two HTTP headers like:

X-Token: fooX-Token: bar

The response would be like:

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

Recap

Declare headers withHeader, using the same common pattern asQuery,Path andCookie.

And don't worry about underscores in your variables,FastAPI will take care of converting them.


[8]ページ先頭

©2009-2026 Movatter.jp