Skip to main content

HttpResponse

An HTTP response class with a string as content.

This content can be read, appended to, or replaced.

Attributes

AttributeTypeDescription
streamingboolean = FalseA boolean flag indicating that the response content is not a stream, used by middleware to determine how to process the response.
contentbytesThe body of the HTTP response stored as a bytestring, which can be read, appended to, or replaced.
textstringThe content of the response decoded into a string using the response's charset or utf-8.

Constructor

Signature

def HttpResponse(
content: bytes|str|iterable = b"",
*args: any,
**kwargs: any
) - > null

Parameters

NameTypeDescription
content`bytesstr
*argsanyPositional arguments passed to the base class constructor.
**kwargsanyKeyword arguments passed to the base class constructor.

Methods


serialize()

@classmethod
def serialize() - > bytes

Full HTTP message, including headers, as a bytestring.

Returns

TypeDescription
bytesThe complete HTTP response message formatted for network transmission.

content()

@classmethod
def content(
value: bytes|str|iterable
) - > bytes

Retrieves the response body as a single concatenated bytestring or sets the response body, consuming iterators to ensure the content is repeatable.

Parameters

NameTypeDescription
value`bytesstr

Returns

TypeDescription
bytesThe full body content of the HTTP response.

text()

@classmethod
def text() - > string

Provides the response content decoded into a string using the object's defined charset.

Returns

TypeDescription
stringThe unicode representation of the response body.

write()

@classmethod
def write(
content: bytes|str
) - > null

Appends a bytestring or string to the existing response body, treating the response like a file-like object.

Parameters

NameTypeDescription
content`bytesstr`

Returns

TypeDescription
null

tell()

@classmethod
def tell() - > int

Returns the current size of the response content in bytes.

Returns

TypeDescription
intThe total number of bytes currently stored in the response body.

getvalue()

@classmethod
def getvalue() - > bytes

Returns the entire content of the response body as a single bytestring, mimicking the BytesIO interface.

Returns

TypeDescription
bytesThe complete accumulated response body.

writable()

@classmethod
def writable() - > boolean

Indicates that the response object supports write operations.

Returns

TypeDescription
booleanAlways returns True to signal file-like write support.

writelines()

@classmethod
def writelines(
lines: iterable
) - > null

Appends a sequence of lines or data chunks to the response body by calling write() for each element.

Parameters

NameTypeDescription
linesiterableAn iterable of strings or bytestrings to be appended to the response.

Returns

TypeDescription
null