Hcs.Request

hcs · API reference

Lazy request accessors.

Server.request stores the method, target, version, headers, assigns, and body reader. Use this module to read headers, query parameters, form fields, the path, the body, and typed request metadata. Header and body work is lazy, so handlers pay only for the parts they inspect.

Lazy request accessors.

HCS parses the request method, target, and protocol version eagerly. Headers, query parameters, form fields, and bodies are accessed through this module so handlers pay only for what they inspect.

Example:

let handler req =
  match Hcs.Request.query req "page" with
  | Some page -> Hcs.Response.text page
  | None -> Hcs.Response.bad_request ()
type t = Server.request

Alias for the server request type.

val meth : t -> Method.t
val target : t -> string
val version : t -> Server.protocol_version
val assigns : t -> Assigns.t

Eager request metadata.

val with_assign : t -> 'a Assigns.key -> 'a -> t

with_assign req key value returns req with value attached under key in its assigns. A plug uses this to hand typed per-request data (the current account, a parsed body, …) to downstream handlers, which read it back with assign.

val with_assigns : t -> Assigns.t -> t

with_assigns req a replaces the request's assigns wholesale.

val assign : t -> 'a Assigns.key -> 'a option

assign req key is the value attached under key, if any — the counterpart to with_assign.

val header : t -> string -> string option
val header_multi : t -> string -> string list
val headers : t -> (string * string) list
val has_header : t -> string -> bool
val set_header : t -> string -> string -> t
val content_type : t -> string option
val content_length : t -> int64 option
val host : t -> string option
val accept : t -> string option
val authorization : t -> string option
val is_keep_alive : t -> bool
val accepts_json : t -> bool
val accepts_html : t -> bool

Header helpers. Header lookup is case-insensitive.

val path : t -> string
val query_string : t -> string option
val parse_query_string : string -> (string * string) list
val query_params : t -> (string * string) list
val query : t -> string -> string option
val query_or : default:string -> t -> string -> string
val query_all : t -> string -> string list
val query_int : t -> string -> int option
val query_int_or : default:int -> t -> string -> int
val query_bool : t -> string -> bool option
val query_bool_or : default:bool -> t -> string -> bool
val query_float : t -> string -> float option
val query_float_or : default:float -> t -> string -> float

Target and query-string helpers.

val is_get : t -> bool
val is_post : t -> bool
val is_put : t -> bool
val is_delete : t -> bool
val is_patch : t -> bool
val is_head : t -> bool
val is_options : t -> bool
val is_safe : t -> bool
val is_idempotent : t -> bool

Method predicates.

val body : t -> string
val body_stream : t -> unit -> Cstruct.t option
val close_body : t -> unit
val with_body : t -> string -> t
val body_string : t -> string
val body_length : t -> int
val has_body : t -> bool

Body helpers. body materializes the body; body_stream consumes chunks.

val form_decode : string -> string
val parse_form_body : string -> (string * string) list
val form_data : t -> (string * string) list
val form_data_from_body : string -> (string * string) list
val form_field : t -> string -> string option
val form_field_or : default:string -> t -> string -> string
val form_int : t -> string -> int option
val form_int_or : default:int -> t -> string -> int

application/x-www-form-urlencoded helpers.