<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Multipart (http-multipart-formdata.Http_multipart_formdata.Multipart)</title><link rel="stylesheet" href="../../../odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 1.5.2"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div class="content"><header><nav><a href="../index.html">Up</a> – <a href="../../index.html">http-multipart-formdata</a> &#x00BB; <a href="../index.html">Http_multipart_formdata</a> &#x00BB; Multipart</nav><h1>Module <code>Http_multipart_formdata.Multipart</code></h1><nav class="toc"><ul><li><a href="#types">Types</a></li><li><a href="#parse">Parse</a></li><li><a href="#pretty-printers">Pretty Printers</a></li><li><a href="#equals">Equals</a></li></ul></nav></header><section><header><h3 id="types"><a href="#types" class="anchor"></a>Types</h3></header><dl><dt class="spec module" id="module-Map"><a href="#module-Map" class="anchor"></a><code><span class="keyword">module</span> Map : Stdlib.Map.S <span class="keyword">with</span> <span class="keyword">type</span> <a href="index.html#module-Map">Map</a>.key = string</code></dt><dd><p>An ocaml <code>Stdlib</code> Map with <code>string</code> as key.</p></dd></dl><dl><dt class="spec module" id="module-Part"><a href="#module-Part" class="anchor"></a><code><span class="keyword">module</span> <a href="Part/index.html">Part</a> : <span class="keyword">sig</span> ... <span class="keyword">end</span></code></dt><dd><p>Represents a parsed multipart part. A part corresponds to a submitted form field data in a HTTP request.</p></dd></dl><dl><dt class="spec type" id="type-t"><a href="#type-t" class="anchor"></a><code><span class="keyword">type</span> t</code><code> = <span><span><a href="Part/index.html#type-t">Part.t</a> list</span> <a href="index.html#module-Map">Map</a>.t</span></code></dt><dd><p>Represents a parsed HTTP <code>multipart/form-data</code> request as a <code>key/value</code> map. Submitted form field name is the key value.</p><p>A key may be associated in zero or more values.</p></dd></dl><dl><dt class="spec exception" id="exception-Multipart"><a href="#exception-Multipart" class="anchor"></a><code><span class="keyword">exception</span> </code><code><span class="exception">Multipart</span> <span class="keyword">of</span> string</code></dt><dd><p>Represents error while parsing http multipart formdata.</p></dd></dl></section><section><header><h3 id="parse"><a href="#parse" class="anchor"></a>Parse</h3></header><dl><dt class="spec value" id="val-parse"><a href="#val-parse" class="anchor"></a><code><span class="keyword">val</span> parse : <span>content_type_header:string</span> <span>&#45;&gt;</span> <span>body:string</span> <span>&#45;&gt;</span> <a href="index.html#type-t">t</a></code></dt><dd><p><code>parse ~content_type_header ~body</code> returns a parsed HTTP multiparts such that it can be queried using ocaml <code>Stdlib.Map</code> functions.</p><p><code>content_type_header</code> is the HTTP request <code>Content-Type</code> header. Note the value contains both the header name and value. It is used to parse a <code>boundary</code> value.</p><p><code>body</code> is the raw HTTP POST request body content.</p><h5 id="examples"><a href="#examples" class="anchor"></a>Examples</h5><pre><code class="ml">let content_type_header =
  &quot;Content-Type: multipart/form-data; \
   boundary=---------------------------735323031399963166993862150&quot; in
let body =
  [ {||}; {|-----------------------------735323031399963166993862150|}
  ; {|Content-Disposition: form-data; name=&quot;text1&quot;|}; {||}
  ; {|text default|}
  ; {|-----------------------------735323031399963166993862150|}
  ; {|Content-Disposition: form-data; name=&quot;text2&quot;|}; {||}; {|aωb|}
  ; {|-----------------------------735323031399963166993862150|}
  ; {|Content-Disposition: form-data; name=&quot;file1&quot;; filename=&quot;a.txt&quot;|}
  ; {|Content-Type: text/plain|}; {||}; {|Content of a.txt.|}; {||}
  ; {|-----------------------------735323031399963166993862150|}
  ; {|Content-Disposition: form-data; name=&quot;file2&quot;; filename=&quot;a.html&quot;|}
  ; {|Content-Type: text/html|}; {||}
  ; {|&lt;!DOCTYPE html&gt;&lt;title&gt;Content of a.html.&lt;/title&gt;|}; {||}
  ; {|-----------------------------735323031399963166993862150|}
  ; {|Content-Disposition: form-data; name=&quot;file3&quot;; filename=&quot;binary&quot;|}
  ; {|Content-Type: application/octet-stream|}; {||}; {|aωb|}
  ; {|-----------------------------735323031399963166993862150--|} ]
  |&gt; String.concat &quot;\r\n&quot; in
let mp = Multipart.parse ~content_type_header ~body in
let file1_1 = Multipart.Map.find &quot;file1&quot; mp in
let file1_2 =
  [ { Multipart.Part.body= Bytes.of_string &quot;\r\nContent of a.txt.\r\n\r\n&quot;
    ; name= &quot;file1&quot;
    ; content_type= &quot;text/plain&quot;
    ; filename= Some &quot;a.txt&quot;
    ; parameters= Multipart.Map.empty } ] in
Multipart.equal_parts file1_1 file1_2</code></pre><dl><dt>raises Multipart</dt><dd></dd></dl></dd></dl></section><section><header><h3 id="pretty-printers"><a href="#pretty-printers" class="anchor"></a>Pretty Printers</h3></header><dl><dt class="spec value" id="val-pp_parts"><a href="#val-pp_parts" class="anchor"></a><code><span class="keyword">val</span> pp_parts : Stdlib.Format.formatter <span>&#45;&gt;</span> <span><a href="Part/index.html#type-t">Part.t</a> list</span> <span>&#45;&gt;</span> unit</code></dt><dd><p><code>pp_parts fmt parts</code> pretty prints a list of <code>Part.t</code></p></dd></dl><dl><dt class="spec value" id="val-pp"><a href="#val-pp" class="anchor"></a><code><span class="keyword">val</span> pp : Stdlib.Format.formatter <span>&#45;&gt;</span> <a href="index.html#type-t">t</a> <span>&#45;&gt;</span> unit</code></dt><dd><p><code>pp fmt part</code> pretty prints a <code>part</code>.</p></dd></dl></section><section><header><h3 id="equals"><a href="#equals" class="anchor"></a>Equals</h3></header><dl><dt class="spec value" id="val-equal_parts"><a href="#val-equal_parts" class="anchor"></a><code><span class="keyword">val</span> equal_parts : <span><a href="Part/index.html#type-t">Part.t</a> list</span> <span>&#45;&gt;</span> <span><a href="Part/index.html#type-t">Part.t</a> list</span> <span>&#45;&gt;</span> bool</code></dt><dd><p><code>equal_parts parts1 parts2</code> returns <code>true</code> if <code>parts1</code> and <code>parts2</code> are equal, <code>false</code> otherwise.</p></dd></dl><dl><dt class="spec value" id="val-equal"><a href="#val-equal" class="anchor"></a><code><span class="keyword">val</span> equal : <a href="index.html#type-t">t</a> <span>&#45;&gt;</span> <a href="index.html#type-t">t</a> <span>&#45;&gt;</span> bool</code></dt><dd><p><code>equal t1 t2</code> returns <code>true</code> if <code>Part.</code> <code>t1</code> and <code>t2</code> are equal, <code>false</code> otherwise.</p></dd></dl></section></div></body></html>