HttpResponseBody PRO
The HttpResponseBody class represents the body content of an HTTP response.
It can contain text, HTML, binary data, or other forms of content.
HttpResponseBody is typically used with the HttpResponse class to return formatted data to the client.
Overview
When building custom HTTP endpoints with HttpServer, the response body defines what content the client actually receives.
HttpResponseBody provides convenient static factory methods to construct various types of response content:
- Plain text (
text) - HTML content (
html,htmlBody) - Binary data (
data)
Common Use Cases
- Returning plain text (e.g., simple API messages)
- Returning HTML pages for browser display
- Returning binary data such as images, JSON files, or downloadable archives
Static Methods
static text(text: string): HttpResponseBody
Creates a plain-text response body.
Parameters:
Example:
Response example:
static data(data: Data): HttpResponseBody
Creates a response body containing binary data.
Parameters:
Example:
This is useful for sending files, images, or JSON payloads as binary data.
static html(html: string): HttpResponseBody
Creates an HTML response body (standard HTML document).
Parameters:
Example:
When accessed in a browser, the response is rendered as a web page.
static htmlBody(html: string): HttpResponseBody
Creates an HTML body-only response.
Similar to html(), but may exclude full document structure (<html>, <body>, etc.).
This method is often used for partial HTML rendering or embedded HTML fragments.
Parameters:
Example:
