Skip to content

blackbull.response

blackbull.response

HTTP and WebSocket response objects. See docs/guide/requests-and-responses.md for send and streaming contracts.

EventSourceResponse

Bases: StreamingResponse

Format str, UTF-8 bytes or data/event/id/retry mappings as SSE.

Data line endings are normalized. Reject CR/LF in event/id and NUL in id. Defaults to text/event-stream and Cache-Control: no-cache; headers may override.

JSONResponse

Bases: Response

JSON response; return from a handler or pass to BlackBull send.

RedirectResponse

Bases: Response

Redirect with an empty body and Location; default status is 302.

url must be ASCII; percent-encode non-ASCII characters first.

Response

HTTP body, status and headers; return from a handler or pass to BlackBull send.

An arbitrary ASGI host's raw send does not accept Response objects.

__call__(conn, receive, send) async

Send a complete native response when invoked directly.

Normalized Response sends use to_native and bypass this method.

to_native()

Convert this response to the unified native message (one send).

The native-path serialiser: a complete Response becomes a single NativeResponse carrying status, headers, and body — one object, one send. Symmetric with NativeResponse.to_asgi (the boundary conversion); streaming response types drive themselves and are not converted here.

StreamingResponse

Stream from an async iterator; finalize the generator on completion, cancellation or a failed send.

Return from a handler, pass to BlackBull send, or invoke as (conn, receive, send).

WebSocketResponse(content)

Build an ASGI websocket.send event dict from content.

  • str → {'type': 'websocket.send', 'text': content}
  • bytes → {'type': 'websocket.send', 'bytes': content}
  • anything else → JSON-serialised into the text field

Pass the result directly to the ASGI send callable::

await send(WebSocketResponse('hello'))

cookie_header(name, value, path='/', http_only=True)

Build a set-cookie header tuple for response headers; checked, like any field, when the NativeResponse carrying it is built.

wrap_native_send(raw_send)

Normalize handler and middleware emissions to native HTTP messages.

Response objects and the (bytes, status, headers) form become a NativeResponse. Streaming responses use the same per-event converter as ASGI dictionaries. Unknown events and native messages pass through.