Added web.FileResponse to route handler response types documentation.

2026-02-15 09:10:41 -05:00
parent d0be6e6137
commit df285f62d9
2 changed files with 11 additions and 3 deletions
+9 -1
@@ -99,15 +99,18 @@ Routes are matched in registration order (first match wins). Routes defined with
## Response Types ## Response Types
Route handlers support three return types: Route handlers support four return types:
| Return Type | Behavior | | Return Type | Behavior |
|-------------|----------| |-------------|----------|
| `dict` | Automatically serialized to JSON with `application/json` content type. | | `dict` | Automatically serialized to JSON with `application/json` content type. |
| `web.Response` | Returned as-is. Full control over status, headers, body. | | `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. | | `None` | Returns a `204 No Content` response. |
```python ```python
from pathlib import Path
from aiohttp import web from aiohttp import web
from owlbot.api import RouteContext, on_route 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: async def html_page(ctx: RouteContext) -> web.Response:
return web.Response(text="<h1>Hello</h1>", content_type="text/html") 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: # No content:
@on_route("/webhook", methods=["POST"]) @on_route("/webhook", methods=["POST"])
async def incoming_webhook(ctx: RouteContext) -> None: 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 ## Context
@@ -225,7 +225,7 @@ async def joke(ctx: CommandContext) -> None:
### Route Responses ### 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 ```python
from aiohttp import web from aiohttp import web