Skip to content

Server as a function

This example is the simplest possible "server" implementation. Note that we are not spinning up a server-backend here - but the entire application(!) is testable by firing HTTP requests at it as if it were.

Gradle setup

    compile group: "org.http4k", name: "http4k-core", version: "3.248.0"

Code

package cookbook.server_as_a_function

import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status

fun main() {

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

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

    val response = app(request)

    println(response)
}