<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Http_multipart_formdata (http-multipart-formdata.Http_multipart_formdata)</title><link rel="stylesheet" href="../../odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 244b5317d"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> – <a href="../index.html">http-multipart-formdata</a> &#x00BB; Http_multipart_formdata</nav><header class="odoc-preamble"><h1>Module <code><span>Http_multipart_formdata</span></code></h1><p><code>Http_multipart_formdata</code> is a non-blocking, streaming HTTP <code>multipart/formdata</code> parser. Its design is based on two main ideas:</p><ul><li>The parser should stream the results as soon as possible in a non-buffered, non-backtracking manner; and</li></ul><ul><li>The parser input must be non-blocking and incremental in nature.</li></ul><p>The parser implements HTTP <code>multipart/form-data</code> standard as defined in <a href="https://tools.ietf.org/html/rfc7578">RFC 7578</a>.</p></header><nav class="odoc-toc"><ul><li><a href="#types">Types</a></li><li><a href="#mulipart-boundary-parser">Mulipart Boundary parser</a></li><li><a href="#streaming-multipart">Streaming Multipart</a></li><li><a href="#non-streaming-multipart">Non-Streaming Multipart</a></li><li><a href="#part-header">Part header</a><ul><li><a href="#pretty-printers">Pretty Printers</a></li></ul></li></ul></nav><div class="odoc-content"><h2 id="types"><a href="#types" class="anchor"></a>Types</h2><div class="odoc-spec"><div class="spec type" id="type-reader" class="anchored"><a href="#type-reader" class="anchor"></a><code><span><span class="keyword">type</span> reader</span></code></div><div class="spec-doc"><p><code>reader</code> represents a HTTP multipart formdata reader.</p></div></div><div class="odoc-spec"><div class="spec type" id="type-read" class="anchored"><a href="#type-read" class="anchor"></a><code><span><span class="keyword">and</span> read</span><span> = </span><span>[ </span></code><table><tr id="type-read.End" class="anchored"><td class="def constructor"><a href="#type-read.End" class="anchor"></a><code><span>| </span></code><code><span>`End</span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p>The reader has completed reading.</p><span class="comment-delim">*)</span></td></tr><tr id="type-read.Header" class="anchored"><td class="def constructor"><a href="#type-read.Header" class="anchor"></a><code><span>| </span></code><code><span>`Header <span class="keyword">of</span> <a href="#type-part_header">part_header</a></span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p>Multipart part header data.</p><span class="comment-delim">*)</span></td></tr><tr id="type-read.Body" class="anchored"><td class="def constructor"><a href="#type-read.Body" class="anchor"></a><code><span>| </span></code><code><span>`Body <span class="keyword">of</span> <span class="xref-unresolved">Cstruct</span>.t</span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p>Multipart part body data.</p><span class="comment-delim">*)</span></td></tr><tr id="type-read.Body_end" class="anchored"><td class="def constructor"><a href="#type-read.Body_end" class="anchor"></a><code><span>| </span></code><code><span>`Body_end</span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p><code>reader</code> has completed reading the Multipart body data.</p><span class="comment-delim">*)</span></td></tr><tr id="type-read.Awaiting_input" class="anchored"><td class="def constructor"><a href="#type-read.Awaiting_input" class="anchor"></a><code><span>| </span></code><code><span>`Awaiting_input <span class="keyword">of</span> <span><span>[ <span>`Cstruct of <span class="xref-unresolved">Cstruct</span>.t</span> <span>| `Eof</span> ]</span> <span class="arrow">&#45;&gt;</span></span> <a href="#type-read">read</a></span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p>The reader is waiting for it to be provided with input data. This is only returned when <code>`Incremental</code> is chosen as <a href="#type-input"><code>input</code></a>.</p><span class="comment-delim">*)</span></td></tr><tr id="type-read.Error" class="anchored"><td class="def constructor"><a href="#type-read.Error" class="anchor"></a><code><span>| </span></code><code><span>`Error <span class="keyword">of</span> string</span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p>Represents an error in <a href="#type-input"><code>input</code></a> data.</p><span class="comment-delim">*)</span></td></tr></table><code><span> ]</span></code></div><div class="spec-doc"><p><code>read</code> represents both the current state and data read by a <a href="#type-reader"><code>reader</code></a>.</p></div></div><div class="odoc-spec"><div class="spec type" id="type-input" class="anchored"><a href="#type-input" class="anchor"></a><code><span><span class="keyword">and</span> input</span><span> = </span><span>[ </span></code><table><tr id="type-input.Cstruct" class="anchored"><td class="def constructor"><a href="#type-input.Cstruct" class="anchor"></a><code><span>| </span></code><code><span>`Cstruct <span class="keyword">of</span> <span class="xref-unresolved">Cstruct</span>.t</span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p>A bigstring input.</p><span class="comment-delim">*)</span></td></tr><tr id="type-input.Incremental" class="anchored"><td class="def constructor"><a href="#type-input.Incremental" class="anchor"></a><code><span>| </span></code><code><span>`Incremental</span></code></td><td class="def-doc"><span class="comment-delim">(*</span><p>The caller of the library periodically provides input to the parser.</p><span class="comment-delim">*)</span></td></tr></table><code><span> ]</span></code></div></div><div class="odoc-spec"><div class="spec type" id="type-part_header" class="anchored"><a href="#type-part_header" class="anchor"></a><code><span><span class="keyword">and</span> part_header</span></code></div><div class="spec-doc"><p>Represents a parsed multipart part header data.</p></div></div><div class="odoc-spec"><div class="spec type" id="type-boundary" class="anchored"><a href="#type-boundary" class="anchor"></a><code><span><span class="keyword">and</span> boundary</span></code></div><div class="spec-doc"><p>Represents the multipart boundary value.</p></div></div><div class="odoc-spec"><div class="spec type" id="type-field_name" class="anchored"><a href="#type-field_name" class="anchor"></a><code><span><span class="keyword">and</span> field_name</span><span> = string</span></code></div><div class="spec-doc"><p>A form field name</p></div></div><div class="odoc-spec"><div class="spec type" id="type-part_body" class="anchored"><a href="#type-part_body" class="anchor"></a><code><span><span class="keyword">and</span> part_body</span><span> = string</span></code></div><div class="spec-doc"><p>A Multipart body</p></div></div><h2 id="mulipart-boundary-parser"><a href="#mulipart-boundary-parser" class="anchor"></a>Mulipart Boundary parser</h2><div class="odoc-spec"><div class="spec value" id="val-boundary" class="anchored"><a href="#val-boundary" class="anchor"></a><code><span><span class="keyword">val</span> boundary : <span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<a href="#type-boundary">boundary</a>, string)</span> <span class="xref-unresolved">Stdlib</span>.result</span></span></code></div><div class="spec-doc"><p><code>boundary content_type</code> parses <code>content_type</code> to extract <a href="#type-boundary"><code>boundary</code></a> value. <code>content_type</code> is the HTTP request <code>Content-Type</code> header value.</p><pre><code>let content_type =
  &quot;multipart/form-data; \
   boundary=---------------------------735323031399963166993862150&quot;
in
Http_multipart_formdata.boundary content_type</code></pre></div></div><h2 id="streaming-multipart"><a href="#streaming-multipart" class="anchor"></a>Streaming Multipart</h2><p>API to stream multipart parts. Use these functions when you have to handle HTTP form submissions which has large file uploads and at the same time be memory efficient.</p><div class="odoc-spec"><div class="spec value" id="val-reader" class="anchored"><a href="#val-reader" class="anchor"></a><code><span><span class="keyword">val</span> reader : <span>?read_buffer_size:int <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-boundary">boundary</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-input">input</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-reader">reader</a></span></code></div><div class="spec-doc"><p><code>reader ?read_buffer_size boundary input</code> creates reader. The default value for <code>read_buffer_size</code> is 1KB.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-read" class="anchored"><a href="#val-read" class="anchor"></a><code><span><span class="keyword">val</span> read : <span><a href="#type-reader">reader</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-read">read</a></span></code></div><div class="spec-doc"><p><code>read reader</code> returns data read by <code>reader</code>.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-unconsumed" class="anchored"><a href="#val-unconsumed" class="anchor"></a><code><span><span class="keyword">val</span> unconsumed : <span><a href="#type-reader">reader</a> <span class="arrow">&#45;&gt;</span></span> <span class="xref-unresolved">Cstruct</span>.t</span></code></div><div class="spec-doc"><p><code>unconsumed reader</code> returns any leftover data still remaining after <a href="#type-reader"><code>reader</code></a> returns <code>`End</code>.</p></div></div><h2 id="non-streaming-multipart"><a href="#non-streaming-multipart" class="anchor"></a>Non-Streaming Multipart</h2><p>Use these functions if the HTTP form submission is of a relatively small size.</p><div class="odoc-spec"><div class="spec value" id="val-parts" class="anchored"><a href="#val-parts" class="anchor"></a><code><span><span class="keyword">val</span> parts : <span><a href="#type-boundary">boundary</a> <span class="arrow">&#45;&gt;</span></span> <span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><span>(<a href="#type-field_name">field_name</a> * <span>(<a href="#type-part_header">part_header</a> * <a href="#type-part_body">part_body</a>)</span>)</span> list</span>, string)</span> <span class="xref-unresolved">Stdlib</span>.result</span></span></code></div><div class="spec-doc"><p><code>parts boundary http_body</code> returns a list of HTTP multipart parts parsed in <code>http_body</code>.</p><p>The returned parts list is keyed to a form field name so that one can do:</p><pre><code>let parts_kv = parts boundary http_body in
match List.assoc_opt &quot;field1&quot; parts_vk with
| Some v -&gt; ...
| None -&gt; ..</code></pre></div></div><h2 id="part-header"><a href="#part-header" class="anchor"></a>Part header</h2><div class="odoc-spec"><div class="spec value" id="val-name" class="anchored"><a href="#val-name" class="anchor"></a><code><span><span class="keyword">val</span> name : <span><a href="#type-part_header">part_header</a> <span class="arrow">&#45;&gt;</span></span> string</span></code></div><div class="spec-doc"><p><code>name t</code> returns the form field name.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-content_type" class="anchored"><a href="#val-content_type" class="anchor"></a><code><span><span class="keyword">val</span> content_type : <span><a href="#type-part_header">part_header</a> <span class="arrow">&#45;&gt;</span></span> string</span></code></div><div class="spec-doc"><p><code>content_type t</code> returns the part content-type.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-filename" class="anchored"><a href="#val-filename" class="anchor"></a><code><span><span class="keyword">val</span> filename : <span><a href="#type-part_header">part_header</a> <span class="arrow">&#45;&gt;</span></span> <span>string option</span></span></code></div><div class="spec-doc"><p><code>filename t</code> returns the uploaded filename if the multipart is a file.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-find" class="anchored"><a href="#val-find" class="anchor"></a><code><span><span class="keyword">val</span> find : <span>string <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-part_header">part_header</a> <span class="arrow">&#45;&gt;</span></span> <span>string option</span></span></code></div><div class="spec-doc"><p><code>find name t</code> returns the multipart parameter value associated with <code>name</code>.</p></div></div><h4 id="pretty-printers"><a href="#pretty-printers" class="anchor"></a>Pretty Printers</h4><div class="odoc-spec"><div class="spec value" id="val-pp_part_header" class="anchored"><a href="#val-pp_part_header" class="anchor"></a><code><span><span class="keyword">val</span> pp_part_header : <span><span class="xref-unresolved">Stdlib</span>.Format.formatter <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-part_header">part_header</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value" id="val-pp_read_result" class="anchored"><a href="#val-pp_read_result" class="anchor"></a><code><span><span class="keyword">val</span> pp_read_result : <span><span class="xref-unresolved">Stdlib</span>.Format.formatter <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-read">read</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value" id="val-pp_boundary" class="anchored"><a href="#val-pp_boundary" class="anchor"></a><code><span><span class="keyword">val</span> pp_boundary : <span><span class="xref-unresolved">Stdlib</span>.Format.formatter <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-boundary">boundary</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div></div></body></html>