kotlin
val client = HttpClient {
install(JsonFeature) {
serializer = GsonSerializer()
}
install(HttpTimeout) {
requestTimeoutMillis = 5000
}
}
kotlin
val response: HttpResponse = client.get("https://api.example.com/data")
println(response.readText())
kotlin
val response: HttpResponse = client.get("https://api.example.com/data")
val data: MyData = client.receive()
implementation "io.ktor:ktor-client-core:{version}"
implementation "io.ktor:ktor-client-json:{version}"
kotlin
val client = HttpClient {
engine {
addInterceptor(TrustAllCertsInterceptor())
followRedirects = true
}
}
kotlin
val data = MyData("John Doe", 25)
val response = client.post<HttpResponse>("https://api.example.com/data") {
contentType(ContentType.Application.Json)
body = Gson().toJson(data)
}