Hello, why this code block doesn't work ya ? ``` ...
# ktor
d
Hello, why this code block doesn't work ya ?
Copy code
fun exec () {
    GlobalScope.launch {
        var counter = 1
        val httpClient = HttpClient(Apache)
        while (true) {
            val clientResponse = httpClient.use {
                it.call {
                    method = <http://HttpMethod.Post|HttpMethod.Post>
                    url(URL("<https://httpbin.org/post>"))
                    body = TextContent(bodyVal, contentType = ContentType("application", "json"))
                }.response
            }

            val jsonResponseString = String(clientResponse.readBytes())
            println(counter.toString())
            counter += 1
        }
    }
}

suspend fun main () = coroutineScope {
    exec()

    Thread.sleep(1000000)
}
Is it because I use
.use{
?
s
Yes, use closes HttpClient after first loop iteration. Do call { }.use{ }
b
A note for future posts. When you say “[it] doesn’t work” doesn’t really help anyone to help you. I’d highly recommend actually explaining what’s not working (ie, the exception you’re getting, the behavior you expected vs actually got)
✔️ 2