ktor

Ktor

Asynchronous Kotlin framework for server and client HTTP applications

Details

Language / Topic
kotlinKotlin
Category
framework
Compatible Frameworks
ktor

Rules

balanced
- For Kotlin: Install features (plugins) in `Application.module()`: `install(ContentNegotiation) { json() }` for JSON, `install(StatusPages)` for errors.
- Define routes with `routing { get("/path") { call.respond(data) } }` — use `route("/api")` blocks for grouping.
- Use `call.receive<T>()` with `@Serializable` data classes for type-safe request parsing — Ktor uses kotlinx.serialization.
- Use Ktor's built-in `HttpClient` for outgoing requests — configure it with the same plugin system: `install(ContentNegotiation)`.
- For Kotlin: Use `kotlinx.serialization` with the ContentNegotiation plugin for JSON request/response handling.
- Use the StatusPages plugin for centralized exception-to-HTTP-response mapping.
- Structure server apps with `Application.module()` extensions: `configureRouting()`, `configureSecurity()`, `configureSerialization()`.
- Use Ktor Client with `HttpClient(CIO)` for making HTTP requests with the same serialization stack.
- Use `call.receive<T>()` for deserializing request bodies and `call.respond(status, body)` for responses.