0Pricing
Kotlin Academy · Lesson

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

All lessons in this course

  1. Ktor Project Setup: embeddedServer and Application Modules
  2. Routing and Typed Parameters
  3. Content Negotiation and kotlinx.serialization
  4. Authentication Plugins: JWT and Session
← Back to Kotlin Academy