Module Morph__.Response

type body = Piaf.Body.t
type failure = [
| Piaf.Error.t
| `User of string
| `Server of string
| `Auth of string
]
type t = (Piaf.Response.tfailure) Stdlib.result

The core Response.t type

val response_of_failure : failure -> Piaf.Response.t
val response_of_result : t -> Piaf.Response.t
val make : ?⁠version:Piaf.Versions.HTTP.t -> ?⁠headers:Piaf.Headers.t -> ?⁠body:Piaf.Body.t -> Piaf.Status.t -> t

make status headers body creates a response.

val empty : t

empty t an empty response, a starting place to compose an http response.

val add_header : (string * string) -> t -> t

add_header header response returns a copy of t of response with the header tuple added.

val add_headers : (string * string) list -> t -> t

add_header headers response returns a copy of t of response with the headers added.

val set_status : Piaf.Status.t -> t -> t

set_status status response returns a copy of t with the given status.

val set_body : body -> t -> t

set_body body response returns a copy of t with the given body.

val ok : unit -> t

ok is a conventience function to return a 200 OK response.

val text : string -> t

text text_string is a conventience function to return a text response.

val json : string -> t

json json_string is a conventience function to return a JSON response.

val html : string -> t

html markup_string is a conventience function to return a HTML response.

To make it work with Tyxml you can add a simple util like this;

let respond_html = elt => {
  Morph.Response.html(Format.asprintf("%a", Tyxml.Html.pp(), elt))
  |> Lwt.return;
};
val redirect : ?⁠code:int -> string -> t

redirect code target is a conventience function to create a redirect response.

val unauthorized : string -> t

unauthorized message is a conventience function to return a unauthorized response.

val not_found : ?⁠message:string -> unit -> t

not_found message is a conventience function to return a 404 Not found response.

val string_stream : stream:string Lwt_stream.t -> t

static stream is a conventience function to return a stream of strings.

val static : file_path:string -> t Lwt.t

static file_path is a conventience function to return a file.