Skip to content

Response API

Bernardusz edited this page Jun 9, 2026 · 1 revision

πŸ“€ 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.

πŸ“‘ Status Codes

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");
});

πŸ“‘ Content Type

Set the Content-Type header easily:

app.get("/custom", ctx -> {
    ctx.contentType("text/xml").send("<root>Hello</root>");
});

βœ‰οΈ Sending Data

Plain Text

Automatically sets Content-Type to text/plain.

ctx.text("Hello World");

HTML

Automatically sets Content-Type to text/html.

ctx.html("<h1>Welcome</h1>");

JSON

Automatically sets Content-Type to application/json.

ctx.json("{\"message\": \"success\"}");

Binary Data

Sends a raw byte array as application/octet-stream.

byte[] data = ...;
ctx.sendBinary(data);

πŸ“‚ Rendering Files

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");
});

πŸ›  Header Management

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);

🐧 Getting Started

🧩 Core API

πŸ“¦ Features

πŸ›‘οΈ Security & Stability

πŸš€ Performance & Deployment

🀝 Project

Clone this wiki locally