The Ktor HTTP client can be used on different platforms, including JVM, Android, JavaScript, and Native. A specific platform may require a specific engine that processes network requests.
For example, you can use Apache
or Jetty
for JVM applications, OkHttp
or Android
for Android, Curl
for desktop applications targeting Kotlin/Native, and so on. Different engines may have specific features and provide different configuration options.
Apart from the ktor-client-core artifact, the Ktor client requires adding a specific dependency for each engine. For each of the supported platform, you can see the available engines and required dependencies in a corresponding section:
To create the HTTP client with a specific engine, pass an engine class as an argument to the HttpClient constructor. For example, you can create a client with the CIO
engine as follows:
{src="snippets/_misc_client/CioCreate.kt"}
If you call the HttpClient
constructor without an argument, the client will choose an engine automatically depending on the artifacts added in a build script.
{src="snippets/_misc_client/DefaultEngineCreate.kt"}
This can be useful for multiplatform projects. For example, for a project targeting both Android and iOS, you can add the Android dependency to the androidMain
source set and the Darwin dependency to the iosMain
source set. The necessary dependency will be selected at compile time.
You can configure an engine using the engine
method. All engines share several common properties exposed by HttpClientEngineConfig, for example:
{src="snippets/_misc_client/BasicEngineConfigExample.kt" interpolate-variables="true" disable-links="false"}
To learn how to configure a specific engine, see a corresponding section below.
In this section, we'll take a look on engines available for JVM.
The Apache
engine supports HTTP/1.1 and provides multiple configuration options. To use it, follow the steps below:
- Add the
ktor-client-apache
dependency: - Pass the Apache class as an argument to the
HttpClient
constructor:{src="snippets/_misc_client/ApacheCreate.kt"} - To configure an engine, pass settings exposed by ApacheEngineConfig to the
engine
method:{src="snippets/_misc_client/ApacheConfig.kt" interpolate-variables="true" disable-links="false"}
The Java
engine uses the Java HTTP Client introduced in Java 11. To use it, follow the steps below:
- Add the
ktor-client-java
dependency: - Pass the Java class as an argument to the
HttpClient
constructor:{src="snippets/_misc_client/JavaCreate.kt"} - To configure an engine, pass settings exposed by JavaHttpConfig to the
engine
method:{src="snippets/_misc_client/JavaConfig.kt" interpolate-variables="true" disable-links="false"}
The Jetty
engine supports only HTTP/2 and can be configured in the following way:
- Add the
ktor-client-jetty
dependency: - Pass the Jetty class as an argument to the
HttpClient
constructor:{src="snippets/_misc_client/JettyCreate.kt"} - To configure an engine, pass settings exposed by JettyEngineConfig to the
engine
method:{src="snippets/_misc_client/JettyConfig.kt" interpolate-variables="true" disable-links="false"}
In this section, we'll take a look on engines available for JVM/Android and their configurations.
CIO is a fully asynchronous coroutine-based engine that can be used for both JVM and Android platforms. It supports only HTTP/1.x for now. To use it, follow the steps below:
-
Add the
ktor-client-cio
dependency: -
Pass the CIO class as an argument to the
HttpClient
constructor:{src="snippets/_misc_client/CioCreate.kt"}
-
To configure an engine, pass settings exposed by CIOEngineConfig to the
engine
method:{src="snippets/_misc_client/CioConfig.kt" interpolate-variables="true" disable-links="false"}
The Android
engine targets Android and can be configured in the following way:
- Add the
ktor-client-android
dependency: - Pass the Android class as an argument to the
HttpClient
constructor:{src="snippets/_misc_client/AndroidCreate.kt"} - To configure an engine, pass settings exposed by AndroidEngineConfig to the
engine
method:{src="snippets/_misc_client/AndroidConfig.kt" interpolate-variables="true" disable-links="false"}
The OkHttp
engine is based on OkHttp can be configured in the following way:
- Add the
ktor-client-okhttp
dependency: - Pass the OkHttp class as an argument to the
HttpClient
constructor:{src="snippets/_misc_client/OkHttpCreate.kt"} - To configure an engine, pass settings exposed by OkHttpConfig to the
engine
method:{src="snippets/_misc_client/OkHttpConfig.kt" interpolate-variables="true" disable-links="false"}
The Js
engine can be used for JavaScript projects. This engine uses the fetch API for browser applications and node-fetch
for Node.js. To use it, follow the steps below:
-
Add the
ktor-client-js
dependency: -
Pass the
Js
class as an argument to theHttpClient
constructor:import io.ktor.client.* import io.ktor.client.engine.js.* val client = HttpClient(Js)
You can also call the
JsClient
function to get theJs
engine singleton:import io.ktor.client.engine.js.* val client = JsClient()
You can find the full example here: client-engine-js.
In this section, we'll have a look on how to configure engines targeted for Kotlin/Native.
The Darwin
engine targets Darwin-based operating systems (such as macOS, iOS, tvOS, and so on) and uses NSURLSession internally. To use it, follow the steps below:
-
Add the
ktor-client-darwin
dependency: -
Pass the
darwin
class as an argument to theHttpClient
constructor:import io.ktor.client.* import io.ktor.client.engine.darwin.* val client = HttpClient(Darwin)
-
To configure an engine, pass settings exposed by DarwinClientEngineConfig to the
engine
method:{src="snippets/client-engine-darwin/src/nativeMain/kotlin/Main.kt" lines="8-14"}
You can find the full example here: client-engine-darwin.
For desktop platforms, Ktor also provides the Curl
engine. This engine is supported for the following platforms: linuxX64
, macosX64
, mingwX64
. To use the Curl
engine, follow the steps below:
-
Install the curl library.
-
Add the
ktor-client-curl
dependency: -
Pass the
Curl
class as an argument to theHttpClient
constructor:import io.ktor.client.* import io.ktor.client.engine.curl.* val client = HttpClient(Curl)
-
To configure an engine, pass settings exposed by
CurlClientEngineConfig
to theengine
method. The code snippet below shows how to disable SSL verification for testing purposes:{src="snippets/client-engine-curl/src/nativeMain/kotlin/Main.kt" lines="8-12"}
You can find the full example here: client-engine-curl.
To configure engine-specific options in a multiplatform mobile project, you can use expect/actual declarations. Let's demonstrate how to achieve this using a project created in the tutorial:
-
Open the
shared/src/commonMain/kotlin/com/example/kmmktor/Platform.kt
file and add a top-levelhttpClient
function, which accepts a client configuration and returnsHttpClient
:expect fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient
-
Open
shared/src/androidMain/kotlin/com/example/kmmktor/Platform.kt
and add an actual declaration of thehttpClient
function for the Android module:import io.ktor.client.* import io.ktor.client.engine.okhttp.* import java.util.concurrent.TimeUnit actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) { config(this) engine { config { retryOnConnectionFailure(true) connectTimeout(0, TimeUnit.SECONDS) } } }
This example shows how to configure the OkHttp engine but you can also use other engines supported for Android.
-
Open
shared/src/iosMain/kotlin/com/example/kmmktor/Platform.kt
and add an actual declaration of thehttpClient
function for the iOS module:import io.ktor.client.* import io.ktor.client.engine.darwin.* actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(Darwin) { config(this) engine { configureRequest { setAllowsCellularAccess(true) } } }
-
Finally, open
shared/src/commonMain/kotlin/com/example/kmmktor/Greeting.kt
and replace theHttpClient()
constructor with thehttpClient
function call:private val client = httpClient()