glimr/response/response
HTTP Response Helpers
Provides convenience functions for building HTTP responses including HTML, JSON, and error pages. Supports both inline content and file-based templates.
Values
pub fn error(status: Int) -> response.Response(wisp.Body)
Generates an error response with the given HTTP status code. Attempts to load a custom error page from the application’s src/resources/views/errors/{status}.html. If no custom page exists, falls back to the framework’s default error page from the framework’s priv directory with the error.html layout.
This allows applications to override default error pages while maintaining consistent fallback behavior.
Example:
// Returns 404 response with custom or default error page
response.error(404)
// Returns 500 response with custom or default error page
response.error(500)
pub fn header(
response: response.Response(wisp.Body),
key: String,
value: String,
) -> response.Response(wisp.Body)
Adds a header to an existing response. Can be chained to add multiple headers to your response. The key and value are set as HTTP headers.
Example:
response.html("\"This is actually json\"", 200)
|> response.header("content-type", "application/json")
pub fn html(
content: String,
status: Int,
) -> response.Response(wisp.Body)
Sets the view content directly from a string without reading from a file. Useful for rendering complete HTML documents or when the HTML is already loaded in memory.
Example:
let html = "<h1>Hello World</h1>"
response.html(html, 200)
pub fn html_file(
file_path: String,
status: Int,
) -> response.Response(wisp.Body)
Creates a view from a static HTML file. The file path is relative to src/resources/views/ and leading slashes are automatically stripped. Panics if the file doesn’t exist.
Example:
response.html_file("contact/success.html", 200)
pub fn json(
json: json.Json,
status: Int,
) -> response.Response(wisp.Body)
Creates a JSON response from a Json value. Serializes the JSON to a string and sets appropriate content-type headers for JSON responses.
Example:
json.string("This is a json response!")
|> response.json(200)
json.object([
#("name", json.string("John Doe")),
#("email", json.string("johndoe@email.com")),
#("age", json.int(30)),
])
|> response.json(200)