HttpResponse PRO
The HttpResponse class represents an HTTP response object returned by the server to the client.
It defines the response’s status code, headers, and body, and provides convenient factory methods for creating common HTTP responses (e.g., ok, notFound, internalServerError).
HttpResponse is typically used together with HttpResponseBody to send text, HTML, binary data, or files back to the client.
Overview
HttpResponse provides:
- Easy creation of standard HTTP responses (200, 404, 500, etc.)
- Support for custom status codes and reason phrases
- Ability to return text, binary, HTML, or file data
- Custom response headers
- Integration with
FileEntityandDatafor flexible body content
Properties
statusCode: number
The numeric HTTP status code.
Example:
reasonPhrase: string
The reason phrase associated with the status code (e.g., "OK", "Not Found", "Internal Server Error").
Example:
Methods
headers(): Record<string, string>
Returns the headers of the response as a key–value object.
Example:
static ok(body: HttpResponseBody): HttpResponse
Creates a 200 OK response.
Parameters:
Example:
static created(): HttpResponse
Returns a 201 Created response, indicating that a new resource was successfully created.
Example:
static accepted(): HttpResponse
Returns a 202 Accepted response, indicating the request was accepted but not yet processed.
Example:
static movedPermanently(url: string): HttpResponse
Returns a 301 Moved Permanently redirect response.
Parameters:
Example:
static movedTemporarily(url: string): HttpResponse
Returns a 302 Moved Temporarily redirect response.
Parameters:
Example:
static badRequest(body?: HttpResponseBody | null): HttpResponse
Returns a 400 Bad Request response, indicating invalid parameters or malformed input.
Parameters:
Example:
static unauthorized(): HttpResponse
Returns a 401 Unauthorized response, indicating authentication is required.
Example:
static forbidden(): HttpResponse
Returns a 403 Forbidden response, indicating the request is understood but not allowed.
Example:
static notFound(): HttpResponse
Returns a 404 Not Found response when the requested resource does not exist.
Example:
static notAcceptable(): HttpResponse
Returns a 406 Not Acceptable response, indicating the request’s content type is unsupported.
Example:
static tooManyRequests(): HttpResponse
Returns a 429 Too Many Requests response, indicating the client is sending requests too quickly.
Example:
static internalServerError(): HttpResponse
Returns a 500 Internal Server Error response, indicating an unexpected server error occurred.
Example:
static raw(statusCode: number, phrase: string, options?: { headers?: Record<string, string>; body?: Data | FileEntity } | null): HttpResponse
Creates a fully custom response with a specific status code, reason phrase, headers, and body.
Parameters:
Example:
Usage with HttpResponseBody
HttpResponseBody.text(text: string)
Returns a plain-text response body.
HttpResponseBody.html(html: string)
Returns an HTML response body.
HttpResponseBody.data(data: Data)
Returns a binary data response body.
