Skip to content

Conversation

Kilo59
Copy link
Contributor

@Kilo59 Kilo59 commented May 4, 2024

Update documentation example for HTTP middleware to complete type annotations in the signature.

import time
from typing import Awaitable, Callable

from fastapi import FastAPI, Request, Response

app = FastAPI()


@app.middleware("http")
async def add_process_time_header(
    request: Request, call_next: Callable[[Request], Awaitable[Response]]
) -> Response:
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response

@Kilo59 Kilo59 marked this pull request as ready for review May 4, 2024 01:30
@alejsdev alejsdev added the docs Documentation about how to use FastAPI label May 4, 2024
@alejsdev alejsdev changed the title Add annotations to HTTP middleware example 📝 Add annotations to HTTP middleware example May 4, 2024
@tadasgedgaudas
Copy link

looks good 👍

Copy link
Contributor

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🚀

@tiangolo tiangolo merged commit dfe9dde into fastapi:master Jun 17, 2025
28 checks passed
@Kilo59 Kilo59 deleted the patch-1 branch June 18, 2025 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation about how to use FastAPI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants