2

I am following the FastAPI documentation here and trying to implement routing with a middleware. My main contains:

app = FastAPI()


app.include_router(
    SomeService.router,
    prefix="/services",
    tags=["services"]
)


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    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

When sending a request, it is correctly executed and returns the right value but does not contain the value appended through the middleware.

I have tried defining the middleware inside the service route and defining the middleware before app.include_router

0

1 Answer 1

1

The documentation about middleware mentions that if

(...) you want a client in a browser to be able to see [the custom headers], you need to add them to your CORS configurations, using the parameter expose_headers documented in Starlette's CORS docs.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes and i have read that. It is working fine but a mistake on my behalf reading the headers. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.