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
- Authenticating Clients
- Enable Automatic HEAD Responses
- Basic and Form authentication
- Controlling cache headers
- CallId
- Log the client requests
- Client/Server Sessions
- Enable HTTP Compression Facilities
- Easy '304 Not Modified' Responses
- Content conversion based on Content-Type and Accept headers
- Cookie/Header Sessions
- Enable Cross-Origin Resource Sharing (CORS)
- Data Conversion
- Send Headers Automatically
- Digest authentication
- DoubleReceive for request body
- XForwardedHeaderSupport (Reverse Proxy Support)
- Using Freemarker Templates
- JSON support using Gson
- Enable HTTP Strict Transport Security
- Emit HTML with a DSL
- Redirect HTTP requests to HTTPS
- JSON support using Jackson
- JWT and JWK authentication
- LDAP authentication
- Type-safe Routing
- Metrics with Micrometer metrics
- Metrics with Dropwizard metrics
- Using Mustache Templates
- OAuth authentication
- Streaming Movies and Other Content
- Structured Handling of HTTP Requests
- JSON support using kotlinx.serialization
- Session Serializers
- Handle Conversations with Sessions
- Add an URL for shutting down the server
- Serving Static Content
- Handle Exceptions and Customize Status Pages
- Session Storages
- Templates
- Using Thymeleaf Templates
- Session Transformers
- Using Velocity Templates
- Webjars support
- WebSockets
Installing
A feature is installed into the Application by calling the install
function:
fun Application.main() {
install(DefaultHeaders)
install(CallLogging)
install(Routing) {
get("/") {
call.respondText("Hello, World!")
}
}
}
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:
install(Routing) {
get("/") {
call.respondText("Hello, World!")
}
}
we could simply write:
routing {
get("/") {
call.respondText("Hello, World!")
}
}
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.