Quickstart

This simple example demonstrates how to serve and consume HTTP services using http4k. To install, add these dependencies to your Gradle file:

dependencies {
    implementation platform("org.http4k:http4k-bom:3.261.0")
    implementation "org.http4k:http4k-core"
    implementation "org.http4k:http4k-server-netty"
    implementation "org.http4k:http4k-client-apache"
}

The following creates a simple endpoint, binds it to a Netty 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.Netty
import org.http4k.server.asServer

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

    val server = app.asServer(Netty(9000)).start()

    val client = ApacheClient()

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

    println(client(request))

    server.stop()
}

Examples Repo

For fully self-contained examples demonstrates the standout features of http4k, there is a GitHub repository at http4k/examples.

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.