Added web.FileResponse to route handler response types documentation.
+9
-1
@@ -99,15 +99,18 @@ Routes are matched in registration order (first match wins). Routes defined with
|
||||
|
||||
## Response Types
|
||||
|
||||
Route handlers support three return types:
|
||||
Route handlers support four return types:
|
||||
|
||||
| Return Type | Behavior |
|
||||
|-------------|----------|
|
||||
| `dict` | Automatically serialized to JSON with `application/json` content type. |
|
||||
| `web.Response` | Returned as-is. Full control over status, headers, body. |
|
||||
| `web.FileResponse` | Serves a file from disk. Sets content type, length, and caching headers automatically. |
|
||||
| `None` | Returns a `204 No Content` response. |
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
|
||||
from aiohttp import web
|
||||
from owlbot.api import RouteContext, on_route
|
||||
|
||||
@@ -121,6 +124,11 @@ async def api_data(ctx: RouteContext) -> dict:
|
||||
async def html_page(ctx: RouteContext) -> web.Response:
|
||||
return web.Response(text="<h1>Hello</h1>", content_type="text/html")
|
||||
|
||||
# Static file:
|
||||
@on_route("/logo")
|
||||
async def logo(ctx: RouteContext) -> web.FileResponse:
|
||||
return web.FileResponse(Path(__file__).parent / "static" / "logo.png")
|
||||
|
||||
# No content:
|
||||
@on_route("/webhook", methods=["POST"])
|
||||
async def incoming_webhook(ctx: RouteContext) -> None:
|
||||
|
||||
+2
-2
@@ -139,7 +139,7 @@ async def status_page(ctx: RouteContext) -> web.Response:
|
||||
)
|
||||
```
|
||||
|
||||
Returning a `dict` automatically serializes to JSON. A `web.Response` gives full control over the output. See [HTTP Routes](Modules-Routes) for templates, URL building, and more.
|
||||
Returning a `dict` automatically serializes to JSON. A `web.Response` gives full control over the output. A `web.FileResponse` can serve static files directly from disk. See [HTTP Routes](Modules-Routes) for templates, URL building, and more.
|
||||
|
||||
## Context
|
||||
|
||||
@@ -225,7 +225,7 @@ async def joke(ctx: CommandContext) -> None:
|
||||
|
||||
### Route Responses
|
||||
|
||||
Routes can return different response types depending on what's needed. A `dict` becomes JSON, and `web.Response` gives full control over the output:
|
||||
Routes can return different response types depending on what's needed. A `dict` becomes JSON, `web.Response` gives full control over the output, and `web.FileResponse` serves static files from disk:
|
||||
|
||||
```python
|
||||
from aiohttp import web
|
||||
|
||||
Reference in New Issue
Block a user