Hcs.Multipart
hcs · API reference
Multipart form-data parsing.
Use this module for file uploads and form submissions encoded as multipart/form-data. It consumes request body streams and returns parsed fields/files according to configured limits.
Multipart form data parsing.
(* Non-streaming *)
match Multipart.parse req with
| Ok parts -> Multipart.find_part "name" parts
| Error e -> Response.bad_request (Multipart.error_to_string e)
(* Streaming large files *)
match Multipart.create_parser req with
| Ok parser ->
Multipart.iter_parts (fun part ->
Stream.Async.iter write_chunk part.body
) parser
| Error e -> ...module Hmf = Http_multipart_formdatatype part = {
name : string;
filename : string option;
content_type : string;
data : string;
}type error =
| Missing_content_type
| Not_multipart
| Missing_boundary
| Invalid_boundary of string
| Parse_error of stringval error_to_string : error -> stringval is_multipart : Request.t -> boolval boundary : Request.t -> (Hmf.boundary, error) resultval parse : Request.t -> (part list, error) resultval find_part : string -> part list -> part optionval find_file : string -> part list -> part optionval to_assoc : part list -> (string * part) listtype stream_part = {
name : string;
filename : string option;
content_type : string;
body : Cstruct.t Stream.Async.t;
}type parser = {
reader : Hmf.reader;
body_stream : unit -> Cstruct.t option;
mutable current_state : Hmf.read;
mutable finished : bool;
}val create_parser : Request.t -> (parser, error) resultval advance_state : parser -> Hmf.readval make_body_stream : parser -> Cstruct.t Stream.Async.tval next_part : parser -> stream_part optionval iter_parts : (stream_part -> 'a) -> parser -> (unit, error) resultval fold_parts :
(stream_part -> 'a -> 'a) ->
'a ->
parser ->
('a, error) resultval collect_body : stream_part -> stringval parse_assoc : Request.t -> ((string * part) list, error) result