Skip to content

Quickstart

This simple example demonstates how to serve and consume HTTP services using http4k.

To install, add these dependencies to your Gradle file:

dependencies {
    compile group: "org.http4k", name: "http4k-core", version: "3.255.0"
    compile group: "org.http4k", name: "http4k-server-jetty", version: "3.255.0"
    compile group: "org.http4k", name: "http4k-client-apache", version: "3.255.0"
}

The following creates a simple endpoint, binds it to a Jetty server then starts, queries, and stops it.

package quickstart

import org.http4k.client.ApacheClient
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.server.Jetty
import org.http4k.server.asServer

fun main() {

    val app = { request: Request -> Response(OK).body("Hello, ${request.query("name")}!") }

    val jettyServer = app.asServer(Jetty(9000)).start()

    val request = Request(Method.GET, "http://localhost:9000").query("name", "John Doe")

    val client = ApacheClient()

    println(client(request))

    jettyServer.stop()
}

Single-line CD Bootstrap

Run the single command in the readme of this repo to create a HelloWorld http4k app with a full CD pipeline using Github -> TravisCI -> Heroku.

Why should I use this library?

Presentation about the development of http4k given at the Kotlin London meetup.