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.280.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.lens.Query
import org.http4k.lens.asResult
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()
}

http4k Toolbox

We have developed a set of useful tools for Developers working with the http4k toolset to turbo-charge app development. Collectively, this is known as the http4k Toolbox. These tools include:

  • A Project Wizard that generates entire bootstrap Server and Serverless project source folders - including fully working starter code, build tooling, extra modules and packaging options.
  • From OpenAPI v2 & V3 specification JSON/YAML, generate an entire working http4k Server, Client and Model objects (generated from JSON Schema).
  • Generate Kotlin Data Class definitions from an inputted JSON, YAML, or XML document.

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.