Hi, this might be a silly question, but which is b...
# general-advice
m
Hi, this might be a silly question, but which is better: Ktor Client or Http4k? With Http4k, you can basically run the server as a one-time process, but with Ktor, you have to set up a server, so I was wondering...
a
Neither is better or worse; they each have their own strengths, weaknesses, and idioms that can be appreciated differently by different people. Both require comparable levels of setup, so I'm not sure what had you bouncing off getting ktor started.
Copy code
// http4k
val api = "path" bind Method.GET to { Response(Status.OK) }
api.asServer(SunHttp(8000)).start()

// ktor
embeddedServer(CIO, port = 8000) {
    routing {
        get("/path") {
            call.respond(HttpStatusCode.OK)
        }
    }
}.start(wait = true)
Http4k is most appreciated by functional-oriented developers who enjoy composition, and test-driven developers who understand the importance of an in-memory server. It's JVM-only and doesn't care about koroutines. It supports HTTP clients, and treats both clients and servers as symmetric; to the point where the two are completely interchangeable. Ktor mostly appeals to people who are accustomed to working with heavyweight Java frameworks, but want something that actually makes good use of Kotlin's features. It supports Kotlin multi-platform, and requires koroutines.
m
I also prefer Ktor. But I'm not sure yet. I'm in a position where I have to deal with coroutines again, and I find coroutines themselves to be complex. (Personally, because they involve determining when to terminate.)
a
In addition, ktor has a test server that can run in memory. Very useful in a testing environment
r
@mrjimin, in Ktor the framework handles the coroutines termination for you, you never deal with it directly. The APIs get called in a coroutine scope and you can use it to go
async
or call any coroutines you need, I've been developing on it and it's quite practical.