Echo request body back.
(request: Request)
| 82 | |
| 83 | @post("/echo") |
| 84 | async def echo(request: Request) -> Response: |
| 85 | """Echo request body back.""" |
| 86 | # Read body using the receive callable to avoid Litestar's internal caching |
| 87 | body_parts = [] |
| 88 | while True: |
| 89 | message = await request.receive() |
| 90 | body = message.get("body", b"") |
| 91 | if body: |
| 92 | body_parts.append(body) |
| 93 | if not message.get("more_body", False): |
| 94 | break |
| 95 | body = b"".join(body_parts) |
| 96 | # Access headers directly from scope to avoid Litestar's caching |
| 97 | scope_headers = {name.decode("latin-1"): value.decode("latin-1") |
| 98 | for name, value in request.scope.get("headers", [])} |
| 99 | content_type = scope_headers.get("content-type", "application/octet-stream") |
| 100 | return Response(content=body, media_type=content_type, status_code=200) |
| 101 | |
| 102 | |
| 103 | @get("/headers") |