Skip to content

Commit

Permalink
docs: fix documentation about middlewares (sparckles#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox authored Apr 12, 2024
1 parent 03239eb commit 6755606
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions docs_src/src/pages/documentation/api_reference/middlewares.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ Batman was excited to learn that he could add events as functions as well as dec
<Row>
<Col>

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.




Expand All @@ -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' }}
Expand All @@ -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
```


Expand Down

0 comments on commit 6755606

Please sign in to comment.