Using Java 11 HttpClient with Kotlin Coroutines

Carvia Tech | October 27, 2019 | 2 min read | 225 views | java-httpclient

In this article we will explore how Java 11 HttpClient can be utilized for making non-blocking http requests. We will also explore how this callback based chaining can be greatly simplified using Kotlin Coroutines.

Gradle setup

We need to include dependencies for Kotlin Coroutines Core and jdk8 in order to run the examples.

dependencies {

Setting up HttpClient

First of all we will create HttpClient instance using builder by specifying connection timeout, version and redirect strategy.

We will be using Kotlin for this tutorial.

Creating HttpClient - JavaClient.kt
class JavaClient {

    private val logger = LoggerFactory.getLogger(

    private val client: HttpClient = HttpClient.newBuilder()

HttpRequest Without Coroutines

We will be using HttpRequest builder to create a Http Request object and then invoking sendAsync method on the underlying HttpClient to asynchronously execute the request.

sendAsync() method returns a CompletableFuture which can be used to fetch the results later on.

fun fetchUrlWithoutCoroutine() {
    val request = HttpRequest.newBuilder()
    val future: CompletableFuture<String> = client
            .sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .thenApply { t: HttpResponse<String> -> t.body() }
            .thenAccept { htmlBody: String -> println(htmlBody)}

This code is bit hard to understand because it requires chaining of callbacks on the returned results. In the next section we will see how readability if greatly improved by using Kotlin Coroutines with httpClient.

Http Request with Kotlin Coroutines

First of all we need to declare the fetchUrl() method as suspend function.

Making a request - JavaClient.kt
suspend fun fetchUrl(): HttpResponse<String> {
    val request = HttpRequest.newBuilder()
    val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
    return response.await() (1)
1 await() extension is provided by kotlin module kotlinx-coroutines-jdk8. This method Awaits for completion of the completion stage (CompletableFuture) without blocking a thread. Internally it will suspend the execution and return HttpResponse on same or different thread once it is available. This does not return a CompletableFuture.

Here we will utilize the Kotlin coroutine to make HttpRequest and wait for the response.

Kotlin Coroutines for making request
fun singleRequest() {
    runBlocking {
        val result = async(Dispatchers.IO) {
            val data = fetchUrl()
        val data = result.await()

fun main() {

The biggest difference you will observe is that kotlin coroutines based code is much easier to understand than the callback based chaining logic shown earlier.

Top articles in this category:
  1. Java 11 HttpClient with Basic Authentication
  2. HTTP GET request with Java 11 HttpClient - Kotlin
  3. HTTP Head request using Java 11 HttpClient - Kotlin
  4. Rest Assured API Testing Interview Questions
  5. Get distinct words from a given file in Java
  6. SDET: JUnit interview questions for automation engineer
  7. SDET Java Coding Challenges

Find more on this topic:

Recommended books for interview preparation: