An HTTP response class with a string as content.
This content can be read, appended to, or replaced.
Attributes
| Attribute | Type | Description |
|---|
| streaming | boolean = False | A boolean flag indicating that the response content is not a stream, used by middleware to determine how to process the response. |
| content | bytes | The body of the HTTP response stored as a bytestring, which can be read, appended to, or replaced. |
| text | string | The 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
| Name | Type | Description |
|---|
| content | `bytes | str |
| *args | any | Positional arguments passed to the base class constructor. |
| **kwargs | any | Keyword arguments passed to the base class constructor. |
Methods
serialize()
@classmethod
def serialize() - > bytes
Full HTTP message, including headers, as a bytestring.
Returns
| Type | Description |
|---|
bytes | The 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
| Name | Type | Description |
|---|
| value | `bytes | str |
Returns
| Type | Description |
|---|
bytes | The 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
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| content | `bytes | str` |
Returns
tell()
@classmethod
def tell() - > int
Returns the current size of the response content in bytes.
Returns
| Type | Description |
|---|
int | The 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
| Type | Description |
|---|
bytes | The complete accumulated response body. |
writable()
@classmethod
def writable() - > boolean
Indicates that the response object supports write operations.
Returns
| Type | Description |
|---|
boolean | Always 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
| Name | Type | Description |
|---|
| lines | iterable | An iterable of strings or bytestrings to be appended to the response. |
Returns