Repository navigation
Response API
The Response object provides a fluent API to construct and send data back to the client. It handles headers, status codes, and various payload formats.
You can set the HTTP status code using the .status(int) method. This method returns the Response instance, allowing for method chaining.
app.get("/not-found", ctx -> {
ctx.status(404).send("Custom Not Found Page");
});Set the Content-Type header easily:
app.get("/custom", ctx -> {
ctx.contentType("text/xml").send("<root>Hello</root>");
});Automatically sets Content-Type to text/plain.
ctx.text("Hello World");Automatically sets Content-Type to text/html.
ctx.html("<h1>Welcome</h1>");Automatically sets Content-Type to application/json.
ctx.json("{\"message\": \"success\"}");Sends a raw byte array as application/octet-stream.
byte[] data = ...;
ctx.sendBinary(data);The render(String path) method serves static files from your configured static directory. It automatically probes the MIME type and handles path normalization to prevent directory traversal attacks.
app.get("/", ctx -> {
ctx.render("index.html");
});You can set individual headers or multiple headers at once.
// Single header
ctx.header("X-Custom-Header", "Value");
// Batch headers
Map<String, List<String>> myHeaders = Map.of("X-Service", List.of("Levtus"));
ctx.headers(myHeaders);