You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/building-projects/responses.md
+32-14Lines changed: 32 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Basic Responses
4
4
5
-
In any web framework, returning a response can be as simple as returning a string of text or quite complex with all sorts of things like server-side rendering. Right out of the box, View supports returning status codes, headers, and a response without any fancy tooling. A response **must** contain a body (this is a `str`), but may also contain a status (`int`) or headers (`dict[str, str]`). These may be in any order.
5
+
In any web framework, returning a response can be as simple as returning a string of text or quite complex with all sorts of things like server-side rendering. Right out of the box, View supports returning status codes, headers, and a response without any fancy tooling. A response **must** contain a body (this is a `str` or `bytes`), but may also contain a status (`int`) or headers (`dict[str, str]`). These may be in any order.
6
6
7
7
```py
8
8
from view import new_app
@@ -242,32 +242,50 @@ class ListResponse(Response[list]):
242
242
243
243
## Middleware
244
244
245
-
### What is middleware?
245
+
### The Middleware API
246
246
247
-
In view.py, middleware is called right before the route is executed, but **not necessarily in the middle.** However, for tradition, View calls it middleware.
247
+
`Route.middleware` is used to define a middleware function for a route. Like other web frameworks, middleware functions are given a `call_next`. Note that `call_next` is always asynchronous regardless of whether the route is asynchronous.
248
248
249
-
The main difference between middleware in view.py and other frameworks is that in view.py, there is no `call_next` function in middleware, and instead just the arguments that would go to the route.
249
+
```py
250
+
from view import new_app, CallNext
250
251
251
-
!!! question "Why no `call_next`?"
252
+
app = new_app()
252
253
253
-
view.py doesn't use the `call_next` function because of the nature of it's routing system.
254
+
@app.get("/")
255
+
defindex():
256
+
return"my response!"
254
257
255
-
### The Middleware API
258
+
@index.middleware
259
+
asyncdefindex_middleware(call_next: CallNext):
260
+
print("this is called before index()!")
261
+
res =await call_next()
262
+
print("this is called after index()!")
263
+
return res
256
264
257
-
`Route.middleware` is used to define a middleware function for a route.
265
+
app.run()
266
+
```
267
+
268
+
### Response Parsing
269
+
270
+
As shown above, `call_next` returns the result of the route. However, dealing with the raw response tuple might be a bit of a hassle. Instead, you can convert the response to a `Response` object using the `to_response` function:
0 commit comments