// build.gradle.kts:
// implementation("io.ktor:ktor-client-core:2.3.+")
// implementation("io.ktor:ktor-client-cio:2.3.+") // CIO engine, JVM
// implementation("io.ktor:ktor-client-content-negotiation:2.3.+")
// implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.+")
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.coroutines.runBlocking
val client = HttpClient(CIO) {
expectSuccess = true // throw on 4xx/5xx
}
suspend fun fetchPage(url: String): String {
val response: HttpResponse = client.get(url) {
headers {
append("User-Agent", "myapp/1.0")
append("Accept", "application/json")
}
}
return response.bodyAsText()
}
fun main() = runBlocking {
val body = fetchPage("https://api.github.com")
println(body.take(200))
client.close()
}
Create a free account and build your private vault. Share publicly whenever you want.