Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 3.33 KB

http-client_multiplatform.md

File metadata and controls

58 lines (42 loc) · 3.33 KB

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:

Add dependencies {id="add-dependencies"}

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:

  1. To use the Ktor client in common code, add the dependency to ktor-client-core to the commonMain source set in the build.gradle or build.gradle.kts file:

  2. 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 .

Create the client {id="create-client"}

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 .

Code example {id="code-example"}

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.