HTTP GET request with Java 11 HttpClient - Kotlin

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


In this article we will learn how to make HTTP GET request using Java 11 HttpClient Api in sync as well as async fully non-blocking mode.

Java 11 provides HttpClient API that supports fully non-blocking IO for making network HTTP requests.

Making HTTP GET request in sync mode
fun httpGetRequestSync() {
    val httpClient: HttpClient = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(10))
            .build()
    val requestHead = HttpRequest.newBuilder()
            .GET()
            .uri(URI.create("https://www.example.com"))
            .build()
    val httpResponse = httpClient.send(requestHead, BodyHandlers.ofString())
    println("httpResponse statusCode = ${httpResponse.statusCode()}")
    println(httpResponse.body())
}
Making HTTP GET request in Async mode
fun httpGetRequestAsync() {
    val httpClient: HttpClient = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(10))
            .build()
    val requestHead = HttpRequest.newBuilder()
            .GET()
            .uri(URI.create("https://www.example.com"))
            .build()
    httpClient
            .sendAsync(requestHead, BodyHandlers.ofString())
            .thenApply(HttpResponse<String>::body)
            .thenAccept { htmlBody -> println(htmlBody) }
            .join()
}

Kotlin coroutines can be used with HttpClient for making network requests without blocking on threads. We need to add the below Kotlin JDK integration library to make code work.

build.gradle
dependencies {
    compile('org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2')
    compile('org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.3.2')
}

Using await() extension on CompletionStage suspends the current coroutine until the response becomes available. Thus callback based code is converted to more readable sequential code.

HTTP GET using Kotlin Coroutine
suspend fun httpGetRequestCoroutine() {
    val httpClient: HttpClient = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(10))
            .build()
    val requestHead = HttpRequest.newBuilder()
            .GET()
            .uri(URI.create("https://www.example.com"))
            .build()
    val httpResponse = httpClient.sendAsync(requestHead, BodyHandlers.ofString()).await()
    println(httpResponse.body())
}

fun makeHttpGetRequestCoroutine() {
    runBlocking {
        httpGetRequestCoroutine()
    }
}

That’s all for now.


Top articles in this category:
  1. SDET: Rest Assured Interview Questions
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: JUnit interview questions for automation engineer
  4. Java 11 HttpClient with Basic Authentication
  5. HTTP Head request using Java 11 HttpClient - Kotlin
  6. Using Java 11 HttpClient with Kotlin Coroutines
  7. Commonly used Http methods in RESTful services



Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info