Module Http

HTTP request and response types, plus client and server operations.

Request and response bodies are Bytes.Reader<Error> values, so body data can be consumed incrementally.

type Http.Error = String

Error type for HTTP operations (a human-readable message).

Sends an HTTP request and returns the response.

let try url = Url.FromString("https://example.com")
let try (status, headers) body = Http.Fetch(
  ("GET")
  (url)
  (*())
  Bytes.EmptyReader
)
dec Http.Listen : [String] recursive either {
  .incoming (Http.Request, [Http.Response] Result<Http.Error, !>) self,
  .shutdown Result<Http.Error, !>,
}

Http.Listen(address) starts an HTTP server on address, such as "127.0.0.1:8080".

Returns a recursive stream of events:

  • .shutdown result — the server has shut down (e.g. on Ctrl+C or error).
  • .incoming(request, respond) next — a request arrived. Call respond with a Response to send the reply. The result indicates if sending succeeded.
Http.Listen("127.0.0.1:8080").begin.case {
  .shutdown try! => ...,
  .incoming(request, respond) next => {
    let (method, url, headers) body = request
    body.close
    respond((200, *()) Bytes.EmptyReader)
    next.loop
  }
}