Extend Application Capabilities

A Ktor application typically consists of a series of features. You can think of features as functionality that is injected into the request and response pipeline. Usually, an application would have a series of features such as DefaultHeaders which add headers to every outgoingresponse, Routing which allows us to define routes to handle requests, etc.

Table of contents:

Features

Installing

A feature is installed into the Application by calling the install function:

  1. fun Application.main(){
  2. install(DefaultHeaders)
  3. install(CallLogging)
  4. install(Routing){
  5. get("/"){
  6. call.respondText("Hello, World!")
  7. }
  8. }
  9. }

Some common feature such as Routing come with helper functions, which are defined as extension functions to Application, making the codesomewhat more fluent. For instance, instead of writing:

  1. install(Routing){
  2. get("/"){
  3. call.respondText("Hello, World!")
  4. }
  5. }

we could simply write:

  1. routing {
  2. get("/"){
  3. call.respondText("Hello, World!")
  4. }
  5. }

Built-in Features

Ktor comes with a number of ready-made features that can be installed into your application:

Some features might need you to add an extra dependency to your project. See the feature pages for more details.

Custom features

You can develop your own features and reuse them across your Ktor applications. Refer to Advanced Features for more information.