Skip to content

Latest commit

 

History

History
259 lines (204 loc) · 13.3 KB

http-client_engines.md

File metadata and controls

259 lines (204 loc) · 13.3 KB
Learn about engines that process network requests.

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

Add an engine dependency {id="dependencies"}

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:

Create a client with a specified engine {id="create"}

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"}

Default engine {id="default"}

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.

Configure an engine {id="configure"}

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.

JVM {id="jvm"}

In this section, we'll take a look on engines available for JVM.

Apache {id="apache"}

The Apache engine supports HTTP/1.1 and provides multiple configuration options. To use it, follow the steps below:

  1. Add the ktor-client-apache dependency:
  2. Pass the Apache class as an argument to the HttpClient constructor:
    {src="snippets/_misc_client/ApacheCreate.kt"}
  3. 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"}

Java {id="java"}

The Java engine uses the Java HTTP Client introduced in Java 11. To use it, follow the steps below:

  1. Add the ktor-client-java dependency:
  2. Pass the Java class as an argument to the HttpClient constructor:
    {src="snippets/_misc_client/JavaCreate.kt"}
  3. 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"}

Jetty {id="jetty"}

The Jetty engine supports only HTTP/2 and can be configured in the following way:

  1. Add the ktor-client-jetty dependency:
  2. Pass the Jetty class as an argument to the HttpClient constructor:
    {src="snippets/_misc_client/JettyCreate.kt"}
  3. 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"}

JVM and Android {id="jvm-android"}

In this section, we'll take a look on engines available for JVM/Android and their configurations.

CIO {id="cio"}

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:

  1. Add the ktor-client-cio dependency:

  2. Pass the CIO class as an argument to the HttpClient constructor:

    {src="snippets/_misc_client/CioCreate.kt"}

  3. 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"}

Android {id="android"}

The Android engine targets Android and can be configured in the following way:

  1. Add the ktor-client-android dependency:
  2. Pass the Android class as an argument to the HttpClient constructor:
    {src="snippets/_misc_client/AndroidCreate.kt"}
  3. 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"}

OkHttp {id="okhttp"}

The OkHttp engine is based on OkHttp can be configured in the following way:

  1. Add the ktor-client-okhttp dependency:
  2. Pass the OkHttp class as an argument to the HttpClient constructor:
    {src="snippets/_misc_client/OkHttpCreate.kt"}
  3. 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"}

JavaScript {id="js"}

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:

  1. Add the ktor-client-js dependency:

  2. Pass the Js class as an argument to the HttpClient constructor:

    import io.ktor.client.*
    import io.ktor.client.engine.js.*
    
    val client = HttpClient(Js)

    You can also call the JsClient function to get the Js engine singleton:

    import io.ktor.client.engine.js.*
    
    val client = JsClient()

You can find the full example here: client-engine-js.

Native {id="native"}

In this section, we'll have a look on how to configure engines targeted for Kotlin/Native.

Darwin {id="darwin"}

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:

  1. Add the ktor-client-darwin dependency:

  2. Pass the darwin class as an argument to the HttpClient constructor:

    import io.ktor.client.*
    import io.ktor.client.engine.darwin.*
    
    val client = HttpClient(Darwin)
  3. 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.

Curl {id="curl"}

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:

  1. Install the curl library.

  2. Add the ktor-client-curl dependency:

  3. Pass the Curl class as an argument to the HttpClient constructor:

    import io.ktor.client.*
    import io.ktor.client.engine.curl.*
    
    val client = HttpClient(Curl)
  4. To configure an engine, pass settings exposed by CurlClientEngineConfig to the engine 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.

Example: How to configure an engine in a multiplatform mobile project {id="mpp-config"}

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:

  1. Open the shared/src/commonMain/kotlin/com/example/kmmktor/Platform.kt file and add a top-level httpClient function, which accepts a client configuration and returns HttpClient:

    expect fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient
  2. Open shared/src/androidMain/kotlin/com/example/kmmktor/Platform.kt and add an actual declaration of the httpClient 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.

  3. Open shared/src/iosMain/kotlin/com/example/kmmktor/Platform.kt and add an actual declaration of the httpClient 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)
          }
       }
    }
  4. Finally, open shared/src/commonMain/kotlin/com/example/kmmktor/Greeting.kt and replace the HttpClient() constructor with the httpClient function call:

    private val client = httpClient()