Kotlin:
Sending an HTTP request

How to:

Kotlin makes HTTP requests straightforward. Here’s a basic example using khttp, a user-friendly library:

import khttp.get

fun main() {
    val response = get("https://api.github.com/users/octocat/orgs")
    println(response.text)
}

Output:

[{"login":"octo-org","id":583231,"url":"https://api.github.com/orgs/octo-org", ...}]

For more robust needs, here’s a snippet using ktor, a Kotlin framework, to asynchronously fetch data:

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*

suspend fun main() {
    val client = HttpClient(CIO)
    val response: String = client.get("https://api.github.com/users/octocat/orgs")
    println(response)
    client.close()
}

Output similar to the first example.

Deep Dive

The khttp library is a convenient tool, modeled after Python’s requests. It’s great for quick scripts but hasn’t been actively maintained. ktor is a newer, active project by JetBrains, designed with coroutines for asynchronous operations. It’s meant for scalable apps. Both handle HTTP requests but serve different use cases.

Historically, HTTP requests in Kotlin were done with Java libraries like HttpURLConnection or Apache’s HttpClient. These are still valid but are more verbose and lack Kotlin’s language features.

As for implementation, remember to handle common HTTP errors and read the response code. You’ll also want to use try-catch for network exceptions and might need to work with headers and query parameters.

See Also