Code example: client-mpp
The Ktor client can be used in multiplatform projects and supports Android, JavaScript, and Native platforms.The Ktor HTTP client can be used in multiplatform projects and supports the following platforms:
- JVM
- Android
- JavaScript
- Native
To use the Ktor HTTP client in your project, you need to add at least two dependencies: a client dependency and an engine dependency. For a multiplatform project, you need to add these dependencies as follows:
-
To use the Ktor client in common code, add the dependency to
ktor-client-core
to thecommonMain
source set in thebuild.gradle
orbuild.gradle.kts
file: -
Add an engine dependency for the required platform to the corresponding source set. For Android, you can add the Android engine dependency to the
androidMain
source set:For iOS, you need to add the Darwin engine dependency to
iosMain
:To learn which engines are supported for each platform, see .
To create the client in a multiplatform project, call the HttpClient constructor in a project's common code:
{src="snippets/_misc_client/DefaultEngineCreate.kt"}
In this code snippet, the HttpClient
constructor doesn't accept an engine as a parameter: the client will choose an engine for the required platform depending on the artifacts added in a build script.
If you need to adjust an engine configuration for specific platforms, pass a corresponding engine class as an argument to the HttpClient
constructor and configure an engine using the engine
method, for example:
{src="snippets/_misc_client/AndroidConfig.kt" interpolate-variables="true" disable-links="false"}
You can learn how to configure all engine types from .
The mpp/client-mpp project shows how to use a Ktor client in a multiplatform application. This application works on the following platforms: Android
, iOS
, JavaScript
, and macosX64
.