Chapter 36: a service and its HTTP client in one process, built
The client side of HTTP: HTTPClient is a closeable the try owns, send gives back a Result, and a program can run a service and its caller in one process on a port the operating system chose - the shape of EchoInProcess again. This lesson builds the service, a function that calls it three ways, and the program that runs both, on the loopback address only. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-serve-and-call.
What you'll build
The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.
#!ek9 defines module book.network.serveandcall defines function describe() -> result as Result of (HTTPClientResponse, HTTPError) stdout <- Stdout() if result.isOk() response <- result.ok() stdout.println(`${response.status()} ${response.header("Content-Type")}: ${response.bodyText("UTF-8")}`) else if result.isError() stdout.println(`not sent: ${result.error()}`) callGreeting() -> port as Integer stdout <- Stdout() base <- Networking().urlOf(`http://127.0.0.1:${port}/greeting`) get <- HTTPMethod("GET") try -> client <- HTTPClient() describe(client.send(HTTPClientRequest(get, base.withPathSegment("Ada")))) missing <- client.send(HTTPClientRequest(get, base.resolve("/nothing"))) if missing.isOk() stdout.println(`${missing.ok().status()} for /nothing`) describe(client.send(HTTPClientRequest(HTTPMethod("TRACE"), base))) defines service Greeting for :/greeting open greet() as GET for :/{name} -> personName as String :=: PATH "name" <- response as HTTPResponse: (who: personName) with trait of HTTPResponse override status() as pure <- rtn as Integer: 200 override content() <- rtn as Bytes: Bytes().withUtf8(`hello ${who}`) override contentType() as pure <- rtn as String: "text/plain" default operator ? defines application GreetingApp register Greeting() defines program ServeAndCall() with application of GreetingApp properties <- NetworkProperties(0, true) try -> httpServer <- HTTP(properties) caller <- (properties) is HTTPReadyHandler as function callGreeting(properties.port) server.stop() httpServer.serveUntilComplete(caller) //EOF
Step 1: describe — a function
Choose Code > New Construct > Function…
describe is the function's name. Add one parameter, result of type Result of (HTTPClientResponse, HTTPError). Choose Preview.
The preview shows describe about to be written. Choose Apply.
Select the describe() line and, above it, type the blank line the book opens its first function with. Saves when typed.
Select the stub, from require result? down to its //AI line, and type describe's own return and work, as the book writes them. Saves when typed.
Step 2: callGreeting — a function
Choose Code > New Construct > Function…
callGreeting is the function's name. Add one parameter, port of type Integer. Choose Preview.
The preview shows callGreeting about to be written. Choose Apply.
Select the stub, from require port? down to its //AI line, and type callGreeting's own return and work, as the book writes them. Saves when typed.
Step 3: Greeting — a service
Choose Code > New Construct > Service…
Greeting is the service's name and /greeting the base URI every route sits under. The route's parameter personName takes its value from the path variable name. The Routes table holds greet, GET /{name}, taking personName String PATH name. Choose Preview.
The preview shows Greeting about to be written, with its route, answering with an HTTPResponse the tool wrote. Choose Apply.
Select the Greeting line and, above it, type the blank line the book opens the block with. Saves when typed.
Select the response, from its first line down to the content line, and type the book's: the capture named who, a greeting, and a text/plain content type. Saves when typed.
Step 4: GreetingApp — an application
Choose Code > New Construct > Application…
GreetingApp is the application's name. It registers the service. The Registrations table holds Greeting. Choose Preview.
The preview shows GreetingApp about to be written, with its register line. Choose Apply.
Select the GreetingApp line and, above it, type the blank line the book opens the block with. Saves when typed.
Step 5: ServeAndCall — a program
Choose Code > New Construct > Program…
ServeAndCall is the program's name. Leave Application empty - a later step says why. Choose Preview.
The preview shows ServeAndCall about to be written. Choose Apply.
Select the ServeAndCall line and, above it, type the blank line the book opens the block with. Saves when typed.
The tool cannot write a program bound to an application that registers a service - the program must create the HTTP server, which no stub can - so it wrote it unbound. Select the stub, from the header down to its //AI line, and type the header with its application and the body: a server on a port the operating system chooses, a ready handler that calls the service and stops it. Saves when typed.
Step 6: Run it
Choose Run > Run Program…
The project's only program is already chosen. Choose Run.
The first request is answered with hello Ada; the second is answered 404, which is still an ok Result; the third is not sent, and the Result carries the HTTPError. Choose Close.
The result
ServeAndCall.ek9 now reads exactly as chapter 36 of EK9 in Depth shows it. Three requests, three meanings: an answer, an answer that is a 404 - an error status is still a response - and a request that was never sent because TRACE is not an HTTPMethod.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_new_construct,ek9_add_application,ek9_add_program,ek9_run_program.


























