According to https://fastapi.tiangolo.com/tutorial/middleware/, we could apply a FastAPI Middleware on async def
endpoints.
Currently I have several non-async def
endpoints, how to apply FastAPI Middleware on non-async def
endpoint? If I still register an async
Middleware, will it work for the non-async def
endpoint ?
For example:
@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
Will the Middleware work properly if call_next
is a non-async def method ?
Thank you.