package tiny_httpd

  1. Overview
  2. Docs
type body = [
  1. | `String of string
  2. | `Stream of byte_stream

Body of a response, either as a simple string, or a stream of bytes.

type t = {
  1. code : Response_code.t;

    HTTP response code. See Response_code.

  2. headers : Headers.t;

    Headers of the reply. Some will be set by Tiny_httpd automatically.

  3. body : body;

    Body of the response. Can be empty.


A response to send back to a client.

val make_raw : ?headers:Headers.t -> code:Response_code.t -> string -> t

Make a response from its raw components, with a string body. Use "" to not send a body at all.

val make_raw_stream : ?headers:Headers.t -> code:Response_code.t -> byte_stream -> t

Same as make_raw but with a stream body. The body will be sent with the chunked transfer-encoding.

val make : ?headers:Headers.t -> (body, Response_code.t * string) Stdlib.result -> t

make r turns a result into a response.

  • make (Ok body) replies with 200 and the body.
  • make (Error (code,msg)) replies with the given error code and message as body.
val make_string : ?headers:Headers.t -> (string, Response_code.t * string) Stdlib.result -> t

Same as make but with a string body.

val make_stream : ?headers:Headers.t -> (byte_stream, Response_code.t * string) Stdlib.result -> t

Same as make but with a stream body.

val fail : ?headers:Headers.t -> code:int -> ('a, unit, string, t) Stdlib.format4 -> 'a

Make the current request fail with the given code and message. Example: fail ~code:404 "oh noes, %s not found" "waldo".

val fail_raise : code:int -> ('a, unit, string, 'b) Stdlib.format4 -> 'a

Similar to fail but raises an exception that exits the current handler. This should not be used outside of a (path) handler. Example: fail_raise ~code:404 "oh noes, %s not found" "waldo"; never_executed()

val pp : Stdlib.Format.formatter -> t -> unit

Pretty print the response.


Innovation. Community. Security.