Ktor 1.4.0 Help

Http Client

In addition to HTTP serving, Ktor also includes a flexible asynchronous HTTP client. This client supports several configurable engines, and has its own set of features.

Calls: Requests and Responses

You can check how to make requests, and how to receive responses in their respective sections.


Remember that requests are asynchronous, but when performing requests, the API suspends further requests and your function will be suspended until done. If you want to perform several requests at once in the same block, you can use launch or async functions and later get the results. For example:

Sequential requests

suspend fun sequentialRequests() { val client = HttpClient() // Get the content of an URL. val firstBytes = client.get<ByteArray>("") // Once the previous request is done, get the content of an URL. val secondBytes = client.get<ByteArray>("") client.close() }

Parallel requests

suspend fun parallelRequests() = coroutineScope<Unit> { val client = HttpClient() // Start two requests asynchronously. val firstRequest = async { client.get<ByteArray>("") } val secondRequest = async { client.get<ByteArray>("") } // Get the request contents without blocking threads, but suspending the function until both // requests are done. val bytes1 = firstRequest.await() // Suspension point. val bytes2 = secondRequest.await() // Suspension point. client.close() }


For more information, check the examples page with some examples.


For more information, check the features page with all the available features.

Last modified: 03 September 2020