I am writing a CLI JVM program with Ktor client (u...
# ktor
e
I am writing a CLI JVM program with Ktor client (using OkHttp engine behind) and found that after using Ktor client to make a HTTP call the program exit with long delay For example, if my program contain these lines:
Copy code
val res = httpClient.get("<https://example.com>").bodyAsText()
println(res)
The
Process finished with exit code 0
will show up after ~1 min of printing out the response body It does not help even I call
httpClient.close()
afterwards What is the reason why it takes so much time to finish the program?
Just tried with CIO engine, the program finished right after printing the response body
c
OkHttp is an Android engine. That will not work in a plain JVM CLI client. https://api.ktor.io/ktor-client/ktor-client-okhttp/io.ktor.client.engine.okhttp/-ok-http/index.html
e
But it should work for JVM as well as OkHttpClient itself is targeting for JVM as well
👍 1
c
Then enable logging in okhttp and check what’s going wrong.
e
If I just write
Copy code
okhttpClient.newCall(Request.Builder().url("<https://example.com>").method("GET", null).build()).execute().use {
            println(it.body?.string())
        }
The
Process finished with exit code 0
prints out quickly after printing out the response body. Looks like an issue in Ktor client
a
You can close the HTTP client to avoid blocking the thread.
e
Even calling
httpClient.close()
does not help
e
Copy code
okHttpClient.shutdown()
e
it doesn't help
a
On my machine, the following program exits after making a request:
Copy code
val client = HttpClient(OkHttp)
client.get("<https://httpbin.org/get>")
client.close()