DEV Community

Amol
Amol

Posted on • Originally published at amolvyawhare.com on

Introduction to Http4k

Couple of weeks ago, I was looking for very simple way to create web server. I found http4k which is a lightweight but fully-featured HTTP toolkit written in pure Kotlin that enables the serving and consuming of HTTP services in a functional and consistent way.

Some of Features of http4k

  • small, written in functional kotlin with zero dependencies.
  • very simple, no magic, no reflections.
  • immutable http model, which make it very easy to test/debug.
  • serverless / server independent

WebServer as Function

http4k is designed as application as function. Web server can be looked as one big function which handles requests.so it’s literally represented as typealias

typealias HttpHandler = (HttpRequest) -> HttpResponse
Enter fullscreen mode Exit fullscreen mode

An endpoint which echos request body can be created just by 2 simple lines.

val app: HttpHandler = { 
    request: Request -> Response(OK).body(request.body) 
}
val server = app.asServer(SunHttp(8000)).start()
Enter fullscreen mode Exit fullscreen mode

Request and Response are immutable objects. SunHttp is container which only meant for development. For production use Netty, Jetty, ApacheServer etc.Because app is just kotlin function, it can be composed very easily.

Filters

http4k provides a Filter function which is basically intercept the Request/Response pipeline and

typealias Filter = (HttpHandler) -> HttpHandler
Enter fullscreen mode Exit fullscreen mode

Below code snippet shows typical composition of HttpHandlers.

val setContentType = Filter { nextHandler -> 
        { request -> nextHandler(request)
            .header("Content-Type", "text/plain")
        }
    }

val composedApp = setContentType.then(app)
Enter fullscreen mode Exit fullscreen mode

Routing and composition

http4k provides some high level functions which helps to compose application which handles each request differently depending upon url, method etc. These functions accepts multiple httpHandlers and return one handler which is composed of them.

val app : HttpHandler = routes(
    "/api" bind GET to apiApp
    "/other" bind routes(
        "/get" bind GET to {_:Request -> Response(OK)}
        "/post" bind POST to otherPostHandlerFn
    )
) 
Enter fullscreen mode Exit fullscreen mode

Conclusion

http4k really stands with the promises they made. API from http4 are written very thoughtfully and are not opinionated. They are easy to reason and work with. Only downside can be no support for coroutine.

To know more about http4k rationale visit http4k/rationale

PS: This is my very first post. For correction please raise pull request at gitlab.

Top comments (0)