Standard library · The web
web/form
Imported as import "web/form" as form, its names are then form.…. Every signature below is the one the checker infers.
Types
Part
Part(name: Str, filename: Str, kind: Str, data: Str)
Functions
fn parse(content_type: Str, body: Str) -> Result(List(Part), Str)
The fields, by the content type: pairs become parts with no file name and no type.
form.parse("application/x-www-form-urlencoded", "a=1&b=2") # => Ok(Cons(form.Part(a, , , 1), Cons(form.Part(b, , , 2), Nil))) form.parse("text/plain", "a=1") # => Err(not a form: text/plain)
fn value(parts: List(Part), name: Str) -> Option(Str)
The first field of that name, as text.
parts = form.parse("application/x-www-form-urlencoded", "a=1&a=2")? form.value(parts, "a") # => Some(1) form.value(parts, "z") # => None
fn file(parts: List(Part), name: Str) -> Option(Part)
The first field of that name that came as a file.
parts = form.parse("application/x-www-form-urlencoded", "a=1")? form.file(parts, "a") # => None
fn values(parts: List(Part), name: Str) -> List(Str)
Every field of that name — a select with several chosen, or a set of checkboxes.
parts = form.parse("application/x-www-form-urlencoded", "c=x&c=y&d=z")? form.values(parts, "c") # => Cons(x, Cons(y, Nil))
fn multipart(content_type: Str, body: Str) -> Result(List(Part), Str)
body = "--B\r\nContent-Disposition: form-data; name=\"k\"\r\n\r\nv\r\n--B--\r\n" form.multipart("multipart/form-data; boundary=B", body) # => Ok(Cons(form.Part(k, , , v), Nil)) form.multipart("multipart/form-data", body) # => Err(multipart/form-data without a boundary)
fn boundary_of(content_type: Str) -> Option(Str)
The boundary parameter, quoted or not.
form.boundary_of("multipart/form-data; boundary=\"a b\"") # => Some(a b) form.boundary_of("multipart/form-data; boundary=xyz") # => Some(xyz) form.boundary_of("text/plain") # => None
Tests
test_multipart_partstest_boundaries_and_refusalstest_urlencoded