Module

http.response

HTTP response with chainable .with_*() transformation API.

Each transformation returns a new Response. Immutable by convention, built incrementally by design.

Classes

Response 28
An HTTP response built through immutable transformations. Construct with a body, then chain ``.wit…

An HTTP response built through immutable transformations.

Construct with a body, then chain.with_*()calls to set status, headers, and cookies. Each call returns a newResponse.

Attributes

Name Type Description
body str | bytes
status int
content_type str
headers tuple[tuple[str, str], ...]
cookies tuple[SetCookie, ...]
render_intent RenderIntent

Methods

body_bytes 0 bytes
Body as bytes.
property
def body_bytes(self) -> bytes
Returns
bytes
text 0 str
Body as string.
property
def text(self) -> str
Returns
str
json 0 Any
Body parsed as JSON. Raises ``ValueError`` if the body is not valid JSON.
property
def json(self) -> Any
Returns
Any
with_status 1 Response
Return a new Response with a different status code.
def with_status(self, status: int) -> Response
Parameters
Name Type Description
status
Returns
Response
with_header 2 Response
Return a new Response with an additional header.
def with_header(self, name: str, value: str) -> Response
Parameters
Name Type Description
name
value
Returns
Response
with_headers 1 Response
Return a new Response with additional headers.
def with_headers(self, headers: Mapping[str, str]) -> Response
Parameters
Name Type Description
headers
Returns
Response
with_content_type 1 Response
Return a new Response with a different content type.
def with_content_type(self, content_type: str) -> Response
Parameters
Name Type Description
content_type
Returns
Response
with_render_intent 1 Response
Return a new Response with response render intent metadata.
def with_render_intent(self, render_intent: RenderIntent) -> Response
Parameters
Name Type Description
render_intent
Returns
Response
with_cookie 8 Response
Return a new Response with an additional Set-Cookie.
def with_cookie(self, name: str, value: str, *, max_age: int | None = None, path: str = '/', domain: str | None = None, secure: bool = False, httponly: bool = True, samesite: str = 'lax') -> Response
Parameters
Name Type Description
name
value
max_age Default:None
path Default:'/'
domain Default:None
secure Default:False
httponly Default:True
samesite Default:'lax'
Returns
Response
without_cookie 2 Response
Return a new Response that deletes a cookie (Max-Age=0).
def without_cookie(self, name: str, path: str = '/') -> Response
Parameters
Name Type Description
name
path Default:'/'
Returns
Response
with_hx_redirect 1 Response
Tell htmx to do a full-page redirect (like entering a URL). Sets the ``HX-Redi…
def with_hx_redirect(self, url: str) -> Response

Tell htmx to do a full-page redirect (like entering a URL).

Sets theHX-Redirectresponse header.

Parameters
Name Type Description
url
Returns
Response
with_hx_location 4 Response
Tell htmx to navigate via AJAX (like clicking a boosted link). When only *url*…
def with_hx_location(self, url: str, *, target: str | None = None, swap: str | None = None, source: str | None = None) -> Response

Tell htmx to navigate via AJAX (like clicking a boosted link).

When only url is provided, setsHX-Locationto the plain URL string. When target, swap, or source are given, sets the header to a JSON object with the specified fields.

Parameters
Name Type Description
url
target Default:None
swap Default:None
source Default:None
Returns
Response
with_hx_retarget 1 Response
Override the target element for this response. Sets the ``HX-Retarget`` respon…
def with_hx_retarget(self, selector: str) -> Response

Override the target element for this response.

Sets theHX-Retargetresponse header to a CSS selector.

Parameters
Name Type Description
selector
Returns
Response
with_hx_reswap 1 Response
Override the swap strategy for this response. Sets the ``HX-Reswap`` response …
def with_hx_reswap(self, strategy: str) -> Response

Override the swap strategy for this response.

Sets theHX-Reswapresponse header. Accepts any valid hx-swap value (e.g. "innerHTML", "outerHTML", "beforeend").

Parameters
Name Type Description
strategy
Returns
Response
with_hx_reselect 1 Response
Override the hx-select for this response. Sets the ``HX-Reselect`` response he…
def with_hx_reselect(self, selector: str) -> Response

Override the hx-select for this response.

Sets theHX-Reselectresponse header to a CSS selector that chooses which part of the response is used for swapping, overriding anyhx-selecton the triggering element.

Parameters
Name Type Description
selector
Returns
Response
with_hx_trigger 1 Response
Trigger a client-side event after the response is received. Sets the ``HX-Trig…
def with_hx_trigger(self, event: str | dict[str, Any]) -> Response

Trigger a client-side event after the response is received.

Sets theHX-Triggerresponse header. Accepts a plain event name string or a dict for events with payloads::

.with_hx_trigger("closeModal")
.with_hx_trigger({"showToast": {"message": "Saved!"}})
Parameters
Name Type Description
event
Returns
Response
with_hx_trigger_after_settle 1 Response
Trigger a client-side event after the settle step. Sets the ``HX-Trigger-After…
def with_hx_trigger_after_settle(self, event: str | dict[str, Any]) -> Response

Trigger a client-side event after the settle step.

Sets theHX-Trigger-After-Settleresponse header.

Parameters
Name Type Description
event
Returns
Response
with_hx_trigger_after_swap 1 Response
Trigger a client-side event after the swap step. Sets the ``HX-Trigger-After-S…
def with_hx_trigger_after_swap(self, event: str | dict[str, Any]) -> Response

Trigger a client-side event after the swap step.

Sets theHX-Trigger-After-Swapresponse header.

Parameters
Name Type Description
event
Returns
Response
with_hx_push_url 1 Response
Push a URL into the browser history stack. Sets the ``HX-Push-Url`` response h…
def with_hx_push_url(self, url: str | bool) -> Response

Push a URL into the browser history stack.

Sets theHX-Push-Urlresponse header. Pass a URL string orFalseto prevent htmx from pushing.

Parameters
Name Type Description
url
Returns
Response
with_hx_replace_url 1 Response
Replace the current URL in the browser location bar. Sets the ``HX-Replace-Url…
def with_hx_replace_url(self, url: str | bool) -> Response

Replace the current URL in the browser location bar.

Sets theHX-Replace-Urlresponse header. Pass a URL string orFalseto prevent htmx from replacing.

Parameters
Name Type Description
url
Returns
Response
with_hx_refresh 0 Response
Tell htmx to do a full page refresh. Sets ``HX-Refresh: true``.
def with_hx_refresh(self) -> Response
Returns
Response
header 2 str | None
Return the first header value matching *name* (case-insensitive).
def header(self, name: str, default: str | None = None) -> str | None
Parameters
Name Type Description
name
default Default:None
Returns
str | None
Redirect 3
A redirect response.

A redirect response.

Attributes

Name Type Description
url str
status int
headers tuple[tuple[str, str], ...]
StreamingResponse 9
A streaming HTTP response that sends chunks progressively. Used for chunked transfer encoding: hea…

A streaming HTTP response that sends chunks progressively.

Used for chunked transfer encoding: headers are sent immediately, then each chunk is sent as an ASGI body message withmore_body=True.

Supports the same.with_*() chainable API as Response so middleware can modify headers/status without knowing the response is streamed.

Attributes

Name Type Description
chunks Iterator[str] | AsyncIterator[str]
status int
content_type str
headers tuple[tuple[str, str], ...]

Methods

with_status 1 StreamingResponse
Return a new StreamingResponse with a different status code.
def with_status(self, status: int) -> StreamingResponse
Parameters
Name Type Description
status
Returns
StreamingResponse
with_header 2 StreamingResponse
Return a new StreamingResponse with an additional header.
def with_header(self, name: str, value: str) -> StreamingResponse
Parameters
Name Type Description
name
value
Returns
StreamingResponse
with_headers 1 StreamingResponse
Return a new StreamingResponse with additional headers.
def with_headers(self, headers: Mapping[str, str]) -> StreamingResponse
Parameters
Name Type Description
headers
Returns
StreamingResponse
with_content_type 1 StreamingResponse
Return a new StreamingResponse with a different content type.
def with_content_type(self, content_type: str) -> StreamingResponse
Parameters
Name Type Description
content_type
Returns
StreamingResponse
header 2 str | None
Return the first header value matching *name* (case-insensitive).
def header(self, name: str, default: str | None = None) -> str | None
Parameters
Name Type Description
name
default Default:None
Returns
str | None
SSEResponse 19
Sentinel response for Server-Sent Events. Wraps an EventStream and requires direct ASGI send/recei…

Sentinel response for Server-Sent Events.

Wraps an EventStream and requires direct ASGI send/receive access (the handler bypasses the normal _send_response path).

Provides no-op.with_*()methods so middleware chains don't crash. SSE headers (text/event-stream, no-cache) are always sent by the SSE handler itself; any middleware header modifications are ignored.

Attributes

Name Type Description
event_stream Any
kida_env Any

Methods

with_status 1 SSEResponse
No-op: SSE always sends 200.
def with_status(self, status: int) -> SSEResponse
Parameters
Name Type Description
status
Returns
SSEResponse
with_header 2 SSEResponse
No-op: SSE headers are fixed by the protocol handler.
def with_header(self, name: str, value: str) -> SSEResponse
Parameters
Name Type Description
name
value
Returns
SSEResponse
with_headers 1 SSEResponse
No-op: SSE headers are fixed by the protocol handler.
def with_headers(self, headers: Mapping[str, str]) -> SSEResponse
Parameters
Name Type Description
headers
Returns
SSEResponse
with_content_type 1 SSEResponse
No-op: SSE content type is always text/event-stream.
def with_content_type(self, content_type: str) -> SSEResponse
Parameters
Name Type Description
content_type
Returns
SSEResponse
with_cookie 3 SSEResponse
No-op: SSE responses don't carry Set-Cookie headers.
def with_cookie(self, name: str = '', value: str = '', **kwargs: Any) -> SSEResponse
Parameters
Name Type Description
name Default:''
value Default:''
**kwargs
Returns
SSEResponse
without_cookie 2 SSEResponse
No-op: SSE responses don't carry Set-Cookie headers.
def without_cookie(self, name: str = '', path: str = '/') -> SSEResponse
Parameters
Name Type Description
name Default:''
path Default:'/'
Returns
SSEResponse
with_hx_redirect 1 SSEResponse
No-op: SSE streams cannot set HX-Redirect.
def with_hx_redirect(self, url: str) -> SSEResponse
Parameters
Name Type Description
url
Returns
SSEResponse
with_hx_location 4 SSEResponse
No-op: SSE streams cannot set HX-Location.
def with_hx_location(self, url: str = '', *, target: str | None = None, swap: str | None = None, source: str | None = None) -> SSEResponse
Parameters
Name Type Description
url Default:''
target Default:None
swap Default:None
source Default:None
Returns
SSEResponse
with_hx_retarget 1 SSEResponse
No-op: SSE streams cannot set HX-Retarget.
def with_hx_retarget(self, selector: str) -> SSEResponse
Parameters
Name Type Description
selector
Returns
SSEResponse
with_hx_reswap 1 SSEResponse
No-op: SSE streams cannot set HX-Reswap.
def with_hx_reswap(self, strategy: str) -> SSEResponse
Parameters
Name Type Description
strategy
Returns
SSEResponse
with_hx_reselect 1 SSEResponse
No-op: SSE streams cannot set HX-Reselect.
def with_hx_reselect(self, selector: str = '') -> SSEResponse
Parameters
Name Type Description
selector Default:''
Returns
SSEResponse
with_hx_trigger 1 SSEResponse
No-op: SSE streams cannot set HX-Trigger.
def with_hx_trigger(self, event: str | dict[str, Any] = '') -> SSEResponse
Parameters
Name Type Description
event Default:''
Returns
SSEResponse
with_hx_trigger_after_settle 1 SSEResponse
No-op: SSE streams cannot set HX-Trigger-After-Settle.
def with_hx_trigger_after_settle(self, event: str | dict[str, Any] = '') -> SSEResponse
Parameters
Name Type Description
event Default:''
Returns
SSEResponse
with_hx_trigger_after_swap 1 SSEResponse
No-op: SSE streams cannot set HX-Trigger-After-Swap.
def with_hx_trigger_after_swap(self, event: str | dict[str, Any] = '') -> SSEResponse
Parameters
Name Type Description
event Default:''
Returns
SSEResponse
with_hx_push_url 1 SSEResponse
No-op: SSE streams cannot set HX-Push-Url.
def with_hx_push_url(self, url: str | bool = '') -> SSEResponse
Parameters
Name Type Description
url Default:''
Returns
SSEResponse
with_hx_replace_url 1 SSEResponse
No-op: SSE streams cannot set HX-Replace-Url.
def with_hx_replace_url(self, url: str | bool = '') -> SSEResponse
Parameters
Name Type Description
url Default:''
Returns
SSEResponse
with_hx_refresh 0 SSEResponse
No-op: SSE streams cannot set HX-Refresh.
def with_hx_refresh(self) -> SSEResponse
Returns
SSEResponse