HTTPResponse
Inherits: RefCounted < Object
Builder for constructing HTTP responses in route handlers.
Description
HTTPResponse provides a fluent API for building HTTP responses. It is automatically created by HTTPServer and passed to route handler callbacks. You can set the status code, headers, and body content, or serve files and start Server-Sent Events streams.
HTTPServer.register_route("GET", "/api/data", func(req, res: HTTPResponse):
# Set JSON response
res.set_json({"message": "Hello"})
# Or set custom status and headers
res.set_status(201)
res.add_header("X-Custom", "Value")
res.set_body("Custom response")
# Or serve a file
res.set_file("user://data.json")
# Or start SSE stream
res.start_sse()
)
Tutorials
Properties
|
||
|
||
|
Methods
void |
add_header(name: String, value: String) |
is_sse_response() const |
|
void |
set_content_type(mime_type: String) |
void |
|
void |
set_json(data: Dictionary) |
void |
Property Descriptions
The response body content that will be sent to the client.
Dictionary headers = { "Server": "Blazium-HTTPServer/1.0" } 🔗
Dictionary get_headers()
Dictionary containing all HTTP headers that will be sent with the response.
The HTTP status code for the response. Defaults to 200 (OK).
Method Descriptions
void add_header(name: String, value: String) 🔗
Adds a custom HTTP header to the response. The name is the header name and value is the header value.
bool is_sse_response() const 🔗
Returns true if this response has been configured as a Server-Sent Events stream by calling start_sse().
void set_content_type(mime_type: String) 🔗
Sets the Content-Type header to the specified mime_type. For example: "application/json", "text/html", "image/png".
Configures the response to serve the file at the given path. The MIME type is automatically determined from the file extension. The file will be streamed to the client.
void set_json(data: Dictionary) 🔗
Sets the response body to the JSON representation of data and automatically sets the Content-Type header to "application/json".
void start_sse() 🔗
Configures the response as a Server-Sent Events stream. This sets the appropriate headers (Content-Type: text/event-stream) and keeps the connection open for streaming events. After calling this, use HTTPServer.send_sse_event() to send events to the client.
func _on_sse(ctx: HTTPRequestContext, res: HTTPResponse):
res.start_sse()
# Target the new stream externally via HTTPServer.send_sse_event()