Routing and Typed Parameters
Define routes with path and query parameters and group them with route blocks.
Ktor Routing Basics
Routing in Ktor is a plugin installed via install(Routing) { ... } or the shorthand routing { ... }. Routes are defined using HTTP-method functions: get, post, put, delete, patch.
routing {
get("/hello") { call.respondText("Hello!") }
post("/items") { /* handle POST */ }
}Path Parameters
Define a path parameter with {name}. Access it via call.parameters["name"]. The value is always a String?:
get("/users/{id}") {
val id = call.parameters["id"] ?: return@get call.respondText("Missing id", status = HttpStatusCode.BadRequest)
call.respondText("User: $id")
}