From 675560679d1c96f94607c703d7c63d202afc649a Mon Sep 17 00:00:00 2001 From: Sanskar Jethi <29942790+sansyrox@users.noreply.github.com> Date: Sat, 13 Apr 2024 00:12:26 +0100 Subject: [PATCH] docs: fix documentation about middlewares (#795) --- .../documentation/api_reference/middlewares.mdx | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/docs_src/src/pages/documentation/api_reference/middlewares.mdx b/docs_src/src/pages/documentation/api_reference/middlewares.mdx index 65ac2dfe1..8348ba896 100644 --- a/docs_src/src/pages/documentation/api_reference/middlewares.mdx +++ b/docs_src/src/pages/documentation/api_reference/middlewares.mdx @@ -72,7 +72,14 @@ Batman was excited to learn that he could add events as functions as well as dec - Batman learned to use both sync and async functions for middlewares. He wrote the following code to add a middleware that would execute before and after each request: + Batman learned to use both sync and async functions for middlewares. He wrote the following code to add a middleware that would execute before and after each request. + A before request middleware is a function that executes before each request. It can modify the request object or perform any other operation before the request is processed. + An after request middleware is a function that executes after each request. It can modify the response object or perform any other operation after the request is processed. + + Every before request middleware should accept a request object and return a request object. Every after request middleware should accept a response object and return a response object on happy case scenario. + + The execution of the before request middleware is stopped if any of the before request middleware returns a response object. The response object is returned to the client without executing the after request middleware or the main entry point code. + @@ -85,12 +92,12 @@ Batman was excited to learn that he could add events as functions as well as dec @app.before_request("/") async def hello_before_request(request: Request): request.headers["before"] = "sync_before_request" - print(request) + return request @app.after_request("/") def hello_after_request(response: Response): response.headers.set("after", "sync_after_request"") - print(response) + return response ``` ```python {{ title: 'typed' }} @@ -99,12 +106,12 @@ Batman was excited to learn that he could add events as functions as well as dec @app.before_request("/") async def hello_before_request(request): request.headers.set("before", "sync_before_request") - print(request) + return request @app.after_request("/") def hello_after_request(response): response.headers.set("after", "sync_after_request"") - print(response) + return response ```