6.10. FastAPI Headers

  • Request headers

  • Response headers

  • Header object

  • Request object

  • Response object

6.10.2. Request

  • Request headers will be strings

>>> from fastapi import FastAPI, Request
>>> app = FastAPI()
>>>
>>>
>>> @app.get('/')
... def index(request: Request):
...     ua = request.headers['User-Agent']
...     host = request.headers['Host']
...     accept = request.headers['Accept']
...     return {'message': 'ok'}
$ curl -D- http://127.0.0.1:8000/
HTTP/1.1 200 OK
date: Fri, 05 Mar 2021 11:36:38 GMT
server: uvicorn
content-length: 16
content-type: application/json

{"message":"ok"}

6.10.3. Response

  • Response headers values must be string

>>> from fastapi import FastAPI, Response
>>> app = FastAPI()
>>>
>>>
>>> @app.get('/')
... def index(response: Response):
...     response.headers['X-API-VERSION'] = '1.0'
...     return {'message': 'ok'}
$ curl -D- http://127.0.0.1:8000/
HTTP/1.1 200 OK
date: Fri, 05 Mar 2021 11:16:51 GMT
server: uvicorn
content-length: 7
content-type: application/json
x-api-version: 1.0

{"message":"ok"}