new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Tutorial · all subjects

middleware

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Middleware definition and request/response flow

A middleware is a function that works with every request before it is processed by any specific path operation, and also with every response before returning it. The middleware takes each request that comes to the application, can do something to that request or run needed code, then passes the request to be processed by the rest of the application. It then takes the response generated by the application, can do something to that response or run needed code, and then returns the response.

Middleware decorator syntax

To create a middleware, use the decorator @app.middleware("http") on top of a function.

Middleware function parameters

The middleware function receives two parameters: the request, and a function call_next that will receive the request as a parameter. The call_next function will pass the request to the corresponding path operation and then return the response generated by that path operation. The middleware can then further modify the response before returning it.

Custom headers with X- prefix

Custom proprietary headers can be added using the X- prefix. However, if you have custom headers that you want a client in a browser to be able to see, you need to add them to CORS configurations using the parameter expose_headers documented in Starlette's CORS docs.

Multiple middleware execution order

When multiple middlewares are added using @app.middleware() decorator or app.add_middleware() method, each new middleware wraps the application, forming a stack. The last middleware added is the outermost, and the first is the innermost. On the request path, the outermost middleware runs first. On the response path, it runs last. For example, with app.add_middleware(MiddlewareA) followed by app.add_middleware(MiddlewareB), the execution order is: Request: MiddlewareB → MiddlewareA → route; Response: route → MiddlewareA → MiddlewareB.

Middleware execution order with dependencies and background tasks

If you have dependencies with yield, the exit code will run after the middleware. If there are any background tasks, they will run after all the middleware.

Using time.perf_counter for request timing

When adding a custom header like X-Process-Time containing the time in seconds that it took to process the request and generate a response, use time.perf_counter() instead of time.time() because it can be more precise for these use cases.

Request object import

You can use from starlette.requests import Request, but FastAPI provides Request as a convenience, and it comes directly from Starlette.

Give your agent this brain