bocadillo.response

Response

Response(self, request:starlette.requests.Request)

The response builder, passed to HTTP views and typically named res.

At the lower-level, Response behaves like an ASGI application instance: it is a callable and accepts receive and send as defined in the ASGI spec.

Parameters

  • request:
  • __the currently processed __::bocadillo.request#Request.

Attributes

  • content (bytes or str): the raw response content.
  • status_code (int): the HTTP status code. If not set, defaults to 200.
  • headers (dict): a case-insensitive dictionary of HTTP headers. If not set, content-type header is set to text/plain.
  • chunked (bool): sets the transfer-encoding header to chunked.
  • attachment (str): a file name that this response should be sent as. This is done by setting the Content-Disposition header, and typically makes the client browser trigger a "Save As…" dialog or download and save the file locally.

html

Write-only property that sets content to the set value and sets the Content-Type header to "text/html".

json

Write-only property that sets content to the JSON-serialized version of the set value, and sets the Content-Type header to `"application/json".

text

Write-only property that sets content to the set value and sets the Content-Type header to "text/plain".

file

Response.file(self, path:str, attach:bool=True)

Send a file asynchronously using aiofiles.

Parameters

  • path (str): a path to a file on this machine.
  • attach (bool, optional): whether to send the file as an attachment. Defaults to True.

background

Response.background(self, func:Callable[..., Coroutine], *args, **kwargs) -> Callable[..., Coroutine]

Register a coroutine function to be executed in the background.

This can be used either as a decorator or a regular function.

Parameters

  • func (callable): a coroutine function.
  • *args, **kwargs: any positional and keyword arguments to pass to func when executing it.

stream

Response.stream(self, func:Callable[[], AsyncIterable[~AnyStr]]=None, raise_on_disconnect:bool=False) -> Callable[[], AsyncIterable[~AnyStr]]

Stream the response.

The decorated function should be a no-argument asynchronous generator function that yields chunks of the response (strings or bytes).

If raise_on_disconnect is True, a ClientDisconnect exception is raised in the generator when it yields a chunk but the client has disconnected. Otherwise, the exception is handled and the stream stops.

event_stream

Response.event_stream(self, func:Callable[[], AsyncIterable[~AnyStr]]=None, **kwargs) -> Callable[[], AsyncIterable[~AnyStr]]

Stream server-sent events.

The decorated function should be a no-argument asynchronous generator function that yields SSE messages (strings or bytes). You can use the server_event helper to ensure that messages are correctly formatted.

This is nearly equivalent to @stream(). The only difference is that this decorator also sets SSE-specific HTTP headers:

  • Cache-Control: no-cache
  • Content-Type: text/event-stream
  • Connection: Keep-Alive

See Also