Ktor 2.3.12 Help

Adding client dependencies

To use the Ktor HTTP client in your project, you need to configure repositories and add the following dependencies:

  • ktor-client-core

    ktor-client-core contains core Ktor client functionality.

  • Engine dependency

    Engines are used to process network requests. Note that a specific platform may require a specific engine that processes network requests.

  • (Optional) Logging dependency

    Provide a logging framework to enable structured and flexible logging capabilities.

  • (Optional) Plugin dependency

    Plugins are used to extend the client with a specific functionality.

Configure the repositories

Before adding Ktor dependencies, you need to configure the repositories for this project:

  • Production

    Production releases of Ktor are available in the Maven central repository. You can declare this repository in your build script as follows:

    repositories { mavenCentral() }
    repositories { mavenCentral() }
  • Early Access Program (EAP)

    To get access to the EAP versions of Ktor, you need to reference the Space repository:

    repositories { maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") } }
    repositories { maven { url "https://maven.pkg.jetbrains.space/public/p/ktor/eap" } }
    <repositories> <repository> <id>ktor-eap</id> <url>https://maven.pkg.jetbrains.space/public/p/ktor/eap</url> </repository> </repositories>

    Note that Ktor EAPs might require the Kotlin dev repository:

    repositories { maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") } }
    repositories { maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" } }
    <repositories> <repository> <id>ktor-eap</id> <url>https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev</url> </repository> </repositories>

Add dependencies

Client dependency

The main client functionality is available in the ktor-client-core artifact. Depending on your build system, you can add it in the following way:

implementation("io.ktor:ktor-client-core:$ktor_version")
implementation "io.ktor:ktor-client-core:$ktor_version"
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-core-jvm</artifactId> <version>${ktor_version}</version> </dependency>

You can replace $ktor_version with the required Ktor version, for example, 2.3.12.

Multiplatform

For a multiplatform project, you can define the Ktor version and the ktor-client-core artifact in the gradle/libs.versions.toml file:

[versions] ktor = "2.3.12" [libraries] ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }

Then, add ktor-client-core as a dependency to the commonMain source set:

sourceSets { commonMain.dependencies { implementation(libs.ktor.client.core) } }

Engine dependency

An engine is responsible for processing network requests. There are different client engines available for various platforms, such as Apache, CIO, Android, iOS, and so on. For example, you can add a CIO engine dependency as follows:

implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation "io.ktor:ktor-client-cio:$ktor_version"
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-cio-jvm</artifactId> <version>${ktor_version}</version> </dependency>

Multiplatform

For a multiplatform project, you need to add a dependency for the required engine to a corresponding source set.

For example, to add the OkHttp engine dependency for Android, you can first define the Ktor version and the ktor-client-okhttp artifact in the gradle/libs.versions.toml file:

[versions] ktor = "2.3.12" [libraries] ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }

Then, add ktor-client-okhttp as a dependency to the androidMain source set:

sourceSets { androidMain.dependencies { implementation(libs.ktor.client.okhttp) } }

For a full list of dependencies required for a specific engine, see Add an engine dependency.

Logging dependency

On JVM, Ktor uses the Simple Logging Facade for Java (SLF4J) as an abstraction layer for logging. SLF4J decouples the logging API from the underlying logging implementation, allowing you to integrate the logging framework that best suits your application's requirements. Common choices include Logback or Log4j. If no framework is provided, SLF4J will default to a no-operation (NOP) implementation, which essentially disables logging.

To enable logging, include an artifact with the required SLF4J implementation, such as Logback:

implementation("ch.qos.logback:logback-classic:$logback_version")
implementation "ch.qos.logback:logback-classic:$logback_version"
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback_version}</version> </dependency>

For more information on logging in Ktor, see Logging.

Plugin dependency

Ktor lets you use additional client functionality (plugins) that is not available by default, such as authorization and serialization. Some of them are provided in separate artifacts. You can learn which dependencies you need from a topic for a required plugin.

Last modified: 13 June 2024