Build HTTP Services and Clients the Functional Kotlin Way with http4k
If you’ve ever felt that building HTTP services in Java or Kotlin involves too much ceremony, too many annotations, or too much framework magic, there’s a different path. What if you could build fully-featured HTTP servers and clients using simple, testable, functional Kotlin—without pulling in a massive framework? That’s exactly what http4k offers.
It’s a toolkit that treats HTTP as a first-class citizen, using pure functions to model requests and responses. The result is servers and clients that are lightweight, incredibly easy to test, and a joy to work with in Kotlin.
What It Does
http4k is a lightweight, functional HTTP toolkit for Kotlin. It provides a consistent, functional model for both serving and consuming HTTP services. At its core, an HTTP service in http4k is just a type alias: typealias HttpHandler = (Request) -> Response. Everything builds from that simple concept—routing, filters, middleware, and HTTP clients are all constructed using composable functions.
Why It’s Cool
The functional approach is the star here. Because everything is just a function, testing becomes trivial. You can test an HttpHandler by calling it with a Request and asserting on the Response—no need to start a server or use mocking frameworks. This simplicity is a huge win for development speed and reliability.
It’s also modular and server-agnostic. Your application logic is decoupled from the underlying server. You can write your handlers once and then run them on a variety of backends (like Apache, Jetty, Netty, or even AWS Lambda) by just changing a single line of code. The same goes for HTTP clients; you can plug in different backends depending on your needs.
The toolkit is "batteries-included but optional." It comes with everything you’d expect—routing, JSON marshalling, templating, websockets, OAuth—but you only pull in what you need. No monolithic framework bloat.
How to Try It
Getting started is straightforward. You can add http4k to your Kotlin project using Gradle or Maven. Here’s a minimal example to create a simple "Hello World" server:
import org.http4k.core.*
import org.http4k.core.Status.Companion.OK
import org.http4k.server.SunHttp
import org.http4k.server.asServer
fun main() {
val app: HttpHandler = { request: Request ->
Response(OK).body("Hello, ${request.query("name") ?: "World"}!")
}
app.asServer(SunHttp(8080)).start()
}
Clone the http4k GitHub repository to explore more examples, check out the comprehensive guide, and see the full set of modules. The docs are excellent and will have you building and testing real services in no time.
Final Thoughts
http4k is a refreshing take on HTTP toolkits. It embraces Kotlin’s strengths—its functional features and expressiveness—to create a developer experience that is both simple and powerful. If you value testability, minimalism, and functional programming, it’s absolutely worth a try. It might just change how you think about building HTTP APIs.
For more projects like this, follow @githubprojects.
Repository: https://github.com/http4k/http4k