Ktor Documentation
repository·main·Indexed 19 days ago
https://github.com/ktorio/ktor-documentationOfficial documentation for the Ktor framework, including Markdown/XML topics and a collection of runnable code snippets. The documentation provides guides and examples for various Ktor plugins, specifically focusing on authentication implementations such as Basic, Bearer, Digest, Form, JWT (HS256 and RS256), LDAP, and Google OAuth.
What's inside Ktor Documentation
- This repository contains the source documentation for the Ktor framework. The documentation is published at https://ktor.io/docs/. The project is built using Writerside and follows a specific organizational structure for topics, code snippets, and configuration.
Overview of Ktor Plugins
mainKtor does not activate any plugins by default. You must explicitly install the functionality you need.
- Built-in Plugins: Ktor provides many plugins out of the box (e.g.,
CORS,Compression,Sessions,CachingHeaders). - Custom Plugins: You can also implement your own custom plugins to extend the pipeline functionality.
- Built-in Plugins: Ktor provides many plugins out of the box (e.g.,
Trace requests in Ktor Server using the CallId plugin
mainThe
CallIdplugin allows you to trace client requests end-to-end by using unique request IDs. The typical workflow involves:- Obtaining a Call ID: Either by retrieving it from an incoming header (e.g., provided by Nginx or Heroku) or by generating a new one on the server.
- Verification: Ktor verifies the ID against a predefined dictionary (defaulting to lowercase alphanumeric and specific symbols) to ensure validity.
- Propagation: You can send the Call ID back to the client in a response header and include it in the MDC (Mapped Diagnostic Context) for logging purposes.
Required dependency:
io.ktor:ktor-server-call-idEnable HTTP/2 in Ktor
mainHTTP/2 support in Ktor is provided via the Jetty or Netty engines. Once the host is correctly configured with an SSL certificate and a suitable ALPN (Application-Layer Protocol Negotiation) implementation, HTTP/2 support is activated automatically.
Key Requirements:
- SSL Certificate: Required because all browsers mandate encrypted connections for HTTP/2.
- ALPN Implementation: Required for protocol negotiation. The method for providing ALPN depends on the engine used (Jetty or Netty).
Understand the Timeout sample endpoints
mainThe Timeout sample demonstrates how to protect a client from long-running or hanging requests using the
HttpTimeoutplugin. It provides two specific endpoints:/timeout: Emulates a long-running process that may hang./proxy: Acts as a proxy to/timeout. It uses theHttpTimeoutplugin to automatically abort the request if the underlying process hangs, protecting the user from indefinite waiting.
Use JTE templates in a Ktor project
mainThis sample project demonstrates how to integrate JTE (Java Template Engine) templates within a Ktor application. It is part of thecodeSnippetsGradle project and serves as a reference implementation for using JTE for server-side rendering in Ktor.Migrating from Express to Ktor (Express project)
mainThis repository contains the Express.js project used as a companion to the official Migrating from Express to Ktor guide. It serves as a practical reference implementation for developers looking to transition their backend services from Express to Ktor by comparing the two implementations.Enable Logging and CallLogging in Ktor
mainThis sample demonstrates how to use two key Ktor features:
- Logging: General logging capabilities within the Ktor framework.
- CallLogging: A specific plugin used to log incoming client requests.
Note: This is a sample project within the
codeSnippetsGradle project designed to show implementation patterns for these features.Supported platforms for Ktor HTTP client
mainThe Ktor HTTP client is designed for multiplatform use and provides support for the following targets:
- JVM
- Android
- JavaScript
- Native (including iOS via Darwin and macOS)
Authentication and authorization in Ktor Server
mainKtor uses the
Authenticationplugin to handle user identity and access control. This plugin allows you to log in users, grant access to specific resources, and securely transmit information. It can be used in conjunction with Sessions to persist user information across multiple HTTP requests.Required dependency:
io.ktor:ktor-server-authWhat is the DoubleReceive plugin and when to use it
mainThe
DoubleReceiveplugin allows you to receive a request body multiple times without triggering aRequestAlreadyConsumedException.By default, Ktor request streams can only be read once. This plugin is useful when a plugin (like
CallLogging) needs to consume the request body for logging or processing, but you still need to access that same body later inside your route handler.What is the PartialContent plugin?
mainThe
PartialContentplugin adds support for handling HTTP range requests. This allows the server to send only a specific portion of an HTTP message back to a client, which is essential for streaming content or resuming partial downloads.Limitations:
- It only works for
HEADandGETrequests. Using theRangeheader with other methods will return a405 Method Not Allowederror. - It only works for responses that have the
Content-Lengthheader defined. - It disables Compression when serving ranges.
- It only works for