multipart API reference
Build and parse multipart/form-data bodies (RFC 7578) - the file-upload counterpart to mime's email multipart. build assembles a list of Parts (form fields and files) into a (contentType, body) pair ready to POST; parse takes a Content-Type header and a body and returns the parts. Bodies are bytes, so binary file content round-trips intact.
Pairs with web / httpd for handling uploads and with http for sending them. parse runs in Go (encoding.parseMultipart); runs on both binaries.
Import with import "multipart.j" as multipart;. See the multipart guide for prose and examples.
Contents
| Name | What it is |
|---|---|
build | Build a form body with a fresh random boundary. |
buildWith | Build a form body with an explicit boundary (deterministic). |
field | A plain text form field. |
file | A file part. |
isFile | Report whether a part is a file (has a filename). |
parse | Parse a multipart/form-data body into its parts. |
text | Decode a part's body as a UTF-8 string (for reading text field values). |
Built | struct: A built form body plus the matching Content-Type header (with the boundary). |
Part | struct: One form part: a field or a file. |
Functions
build
multipart.build(parts as list of Part)Build a form body with a fresh random boundary.
parts- the parts
- Returns
Built - the Content-Type and encoded body
buildWith
multipart.buildWith(parts as list of Part, boundary as string)Build a form body with an explicit boundary (deterministic).
parts- the parts
boundary- the boundary token (must not occur in any part body)
- Returns
Built - the Content-Type and encoded body
field
multipart.field(name as string, value as string)A plain text form field.
name- the field name
value- the field value
- Returns
Part - the part
file
multipart.file(
name as string,
filename as string,
contentType as string,
data as bytes)A file part.
name- the field name
filename- the file name
contentType- the file's Content-Type (e.g. "text/plain")
data- the file content
- Returns
Part - the part
isFile
multipart.isFile(p as Part)Report whether a part is a file (has a filename).
p- the part
- Returns
bool - true if the part carries a filename
parse
multipart.parse(contentType as string, body as bytes)Parse a multipart/form-data body into its parts.
contentType- the Content-Type header (carrying the boundary)
body- the encoded body
- Returns
list of Part - the parts (fields and files)
- Throws
Error - kind "multipart" if the boundary is missing, a body part line starts with the delimiter, the close delimiter is missing, or a part is malformed (no header terminator, a header that is not UTF-8, a header line without a colon, no
Content-Disposition: form-datawith aname); kind "limit" past 1000 parts, a part header over 16 KiB, or a name / filename over 1 KiB
text
multipart.text(p as Part)Decode a part's body as a UTF-8 string (for reading text field values).
p- the part
- Returns
string - the decoded body
Structs
Built
A built form body plus the matching Content-Type header (with the boundary).
def struct Built {
contentType as string,
body as bytes
};contentType- the
multipart/form-data; boundary=...value body- the encoded body
Part
One form part: a field or a file.
def struct Part {
name as string,
filename as string,
contentType as string,
data as bytes
};name- the field name
filename- the file name ("" for a plain field)
contentType- the part's Content-Type ("" for a plain field)
data- the part body