Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

FastAPI Request Headers and Cookies

FastAPI Request Headers and Cookies

FastAPI provides Header and Cookie types to retrieve data from HTTP request headers and cookies.


Header Parameters

Use Header Declare header parameters:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Header

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

Receive the User-Agent header

    user_agent: Annotated

[

str

|

None

,

Header

(

)

]

=

None

,

)

:

   

return

{

"User-Agent"

: user_agent

}

Access the http://127.0.0.1:8000/items/; the returned JSON contains the browser's User-Agent information.


Automatic Conversion of Request Headers

Field names in HTTP request headers use hyphens (such as X-Token), while Python variable names cannot contain hyphens. FastAPI automatically converts this:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Header

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

Python variable names use underscores, which FastAPI automatically converts to the X-Token header

    x_token: Annotated

[

list

[

str

]

|

None

,

Header

(

)

]

=

None

,

)

:

   

return

{

"X-Token values"

: x_token

}

Conversion rules:

Python variable name HTTP request header Description
x_token X-Token Underscores automatically become hyphens
user_agent User-Agent Same as above
content_type Content-Type Same as above

FastAPI automatically converts underscores in variable names _ to hyphens - to match the header. If you need to disable this conversion, set Header(convert_underscores=False)。


Receiving duplicate headers

Some headers may appear multiple times (such as Set-Cookie), and use list type to receive them:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Header

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

Receiving multiple X-Token headers

    x_token: Annotated

[

list

[

str

]

|

None

,

Header

(

)

]

=

None

,

)

:

   

return

{

"X-Token values"

: x_token

}

Request example:

GET /items/ HTTP/1.1

X-Token: foo

X-Token: bar

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


Cookie Parameters

Use Cookie Declaring Cookie parameters:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Cookie

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

Receiving a Cookie named session_token

    session_token: Annotated

[

str

|

None

,

Cookie

(

)

]

=

None

,

)

:

   

return

{

"session_token"

: session_token

}


Using Headers and Cookies together

You can get both headers and Cookies in the same route:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Header

,

Cookie

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

    user_agent: Annotated

[

str

|

None

,

Header

(

)

]

=

None

,

    session_token: Annotated

[

str

|

None

,

Cookie

(

)

]

=

None

,

    ads_id: Annotated

[

str

|

None

,

Cookie

(

)

]

=

None

,

)

:

   

return

{

       

"User-Agent"

: user_agent

,

       

"Session-Token"

: session_token

,

       

"Ads-ID"

: ads_id

,

   

}


Summary