12 - Header Parameters#
Use Header
from FastAPI to work with HTTP headers.
from fastapi import FastAPI, Header
@app.get("/items13/")
async def read_items(user_agent: Annotated[str | None, Header()] = None):
return {"User-Agent": user_agent}
Note that the hyphen in User-Agent
is converted to an underscore when it is converted to a variable user_agent
.
import requests
url = 'http://127.0.0.1:8000'
requests.get(url + '/items13', headers={'User-Agent': 'Michael Moen'}).json()
{'User-Agent': 'Michael Moen'}