Form
provides helpers for parsing application/x-www-form-urlencoded
and multipart/form-data request bodies.
decode-multipart
(Fn [(Ref String a), (Ref String b)] (Array FormPart))
(decode-multipart body boundary)
parses a multipart/form-data body given the
boundary string. Returns an array of FormPart values.
(let [parts (Form.decode-multipart body "boundary123")]
(Array.nth &parts 0))
Each part’s name, optional filename, and content-type are extracted
from its MIME headers. The body field is the raw content between the
part headers and the next boundary delimiter.
decode-multipart-request
(Fn [(Ref Request a)] (Array FormPart))
(decode-multipart-request req)
parses the multipart form body from a
request. Returns an array of FormPart values, or an empty
array if the content type is not multipart/form-data or the boundary
is missing.
(defn upload [req params]
(let [parts (Form.decode-multipart-request req)]
(if (> (Array.length &parts) 0)
(Response.text (fmt "got %d parts" (Array.length &parts)))
(Response.bad-request))))
multipart?
(Fn [(Ref Request a)] Bool)
(multipart? req)
checks whether a request has a multipart/form-data
content type.
parse
(Fn [(Ref String a)] (Result (Map String String) b))
(parse s)
parses a URL-encoded form body into a (Map String String).
(let [data (Form.decode (Request.body req))]
(Map.get &data "username"))
Keys and values are URL-decoded. Duplicate keys keep the last value.
parse-request
a
parses the form body from a request. Returns an empty
map if the content type is not application/x-www-form-urlencoded.