GitHub

WebResponse

API reference for WebResponse.

A response as sent.

Attributes

AttributeTypeDescription
status= status
status_text= status_text

Functions

__init__

__init__(body=None, *, status=200, status_text='', headers=None) -> None
ParameterTypeDescription
bodybytes | str | AsyncIterable[bytes] | None = None
statusint = 200
status_textstr = ''
headersHeaderLines | None = None
def __init__(
    self,
    body: bytes | str | AsyncIterable[bytes] | None = None,
    *,
    status: int = 200,
    status_text: str = "",
    headers: HeaderLines | None = None,
) -> None:
    lines = _lines(headers)
    if isinstance(body, str) and not any(key == "content-type" for key, _ in lines):
        lines = (*lines, ("content-type", "text/plain;charset=UTF-8"))
    super().__init__(lines, body)
    self.status = status
    self.status_text = status_text

json_of

json_of(value, *, status=200) -> WebResponse

A JSON response (Web Response.json).

ParameterTypeDescription
valueobject
statusint = 200

Returns WebResponse

@classmethod
def json_of(cls, value: object, *, status: int = 200) -> WebResponse:
    """A JSON response (Web ``Response.json``)."""
    return cls(
        text_encode(_json_text.stringify(value)),
        status=status,
        headers={"content-type": "application/json"},
    )
get_set_cookie() -> list[str]

Returns list[str]

def get_set_cookie(self) -> list[str]:
    return [value for key, value in self.header_lines if key == "set-cookie"]

get

get(name) -> str | None

Header name, repeated values joined with ", ".

ParameterTypeDescription
namestr

Returns str | None

def get(self, name: str) -> str | None:
    """Header ``name``, repeated values joined with ``", "``."""
    name = name.lower()
    values = [value for key, value in self.header_lines if key == name]
    return ", ".join(values) if values else None

has

has(name) -> bool
ParameterTypeDescription
namestr

Returns bool

def has(self, name: str) -> bool:
    name = name.lower()
    return any(key == name for key, _ in self.header_lines)

read

read() -> bytes

The whole body (it can be read once).

Returns bytes

async def read(self) -> bytes:
    """The whole body (it can be read once)."""
    if self._used:
        raise RuntimeError("body already read")
    self._used = True
    return await _read(self.body)

buffer

buffer() -> bytes

Read the body into memory, so it can be read again (what Web Response.clone allows).

Returns bytes

async def buffer(self) -> bytes:
    """Read the body into memory, so it can be read again (what Web
    ``Response.clone`` allows)."""
    if self._used:
        raise RuntimeError("body already read")
    if not (self.body is None or isinstance(self.body, bytes)):
        self.body = await _read(self.body)
    return self.body or b""

text

text() -> str

Returns str

async def text(self) -> str:
    return (await self.read()).decode("utf-8", "replace")

json

json() -> Any

Returns Any

async def json(self) -> Any:
    return _json_text.parse(await self.read())

chunks

chunks() -> AsyncIterator[bytes]

The body, chunk by chunk.

Returns AsyncIterator[bytes]

def chunks(self) -> AsyncIterator[bytes]:
    """The body, chunk by chunk."""

    self._used = True
    body = self.body
    if body is None or isinstance(body, bytes):
        return _once(body)
    # The body itself, so closing the iterator closes it.
    return aiter(body)