multipart - multipart/form-data build and parse
Import with import "multipart.j" as multipart;. Build and parse multipart/form-data bodies (RFC 7578) - the file-upload counterpart to mime's email multipart. build turns a list of Parts (form fields and files) into a (contentType, body) pair ready to POST; parse turns a Content-Type header and a body back into parts. Bodies are bytes, so binary file content round-trips intact. Pure .j over strings + bytes; runs on both binaries.
import "multipart.j" as multipart;
use encoding;
def parts as list of multipart.Part init [
multipart.field("title", "hello"),
multipart.file("doc", "a.txt", "text/plain", encoding.encode("utf-8", "hi"))
];
def form as multipart.Built init multipart.build($parts);
# POST $form.body with header Content-Type: $form.contentType
def back as list of multipart.Part init multipart.parse($form.contentType, $form.body);Runnable: examples/modules/multipart_demo.j.
Parts
def struct Part { # multipart.Part
name as string, # the field name
filename as string, # the file name ("" for a plain field)
contentType as string, # the part Content-Type ("" for a plain field)
data as bytes # the part body
};| Call | Returns | |
|---|---|---|
multipart.field(name, value) | Part | a plain text field |
multipart.file(name, filename, contentType, data) | Part | a file part (body is bytes) |
multipart.text(p) | string | decode a part's body as UTF-8 (for field values) |
multipart.isFile(p) | bool | whether the part carries a filename |
Building
| Call | Returns | |
|---|---|---|
multipart.build(parts) | Built | encode with a fresh random boundary |
multipart.buildWith(parts, boundary) | Built | encode with an explicit boundary (deterministic) |
Built{ contentType, body } pairs the ready-to-send Content-Type header (carrying the boundary) with the encoded body. Use build normally; buildWith when you need a fixed boundary (tests, reproducibility). Its boundary must follow RFC 2046 (1-70 of 0-9 A-Z a-z '()+_,-./:=? and space, not ending in a space) and must not occur inside any part's data; either fault is an Error{kind: "multipart"}. A boundary that is not an RFC 2045 token (a space or one of (),/:=?) is quoted in contentType. The random boundary from build does not collide in practice.
Parsing
multipart.parse(contentType, body) reads the boundary from the Content-Type header (the boundary parameter, matched as a whole name) and splits the body into Parts. It matches the delimiter only at CRLF--boundary (normalising a leading boundary), so the boundary token appearing inside a line of a file body does not split it. A delimiter line may carry trailing spaces / tabs before its CRLF; the preamble and the epilogue after --boundary-- are dropped.
| Input | Throws |
|---|---|
no boundary parameter, or no closing --boundary-- (a truncated body) | Error{kind: "multipart"} |
a line that starts with --boundary and goes on with other text (RFC 2046 forbids the boundary at the start of any body line, so it is neither delimiter nor data) | Error{kind: "multipart"} |
| a part without a header terminator, a header line without a colon, a header that is not valid UTF-8, or an unterminated quoted parameter | Error{kind: "multipart"} |
a part without Content-Disposition: form-data carrying a name parameter (RFC 7578 4.2; an empty name="" is accepted) | Error{kind: "multipart"} |
more than 1000 parts, a part header over 16 KiB, or a name / filename over 1 KiB | Error{kind: "limit"} |
The parse runs in Go (encoding.parseMultipart), so memory stays a small multiple of the body.
use io;
import "multipart.j" as multipart;
func show(contentType as string, body as bytes) {
def items as list of multipart.Part init multipart.parse($contentType, $body);
for (def p in $items) {
if (multipart.isFile($p)) {
io.printf("file %s: %s, %d bytes\n", $p.name, $p.filename, len($p.data));
} else {
io.printf("field %s = %s\n", $p.name, multipart.text($p));
}
}
}Scope
form-dataonly. The genericmultipart/*(mixed, alternative, related) used in email ismime's job; this is the browser/HTTP upload shape.Content-Disposition/Content-Typeheaders only. Other per-part headers and RFC 2231 extended (name*=) parameter encoding are not parsed;name/filenameare read from theContent-Dispositionparameters only (never fromContent-Typeor another header), andfilenameis the sender's string: take its last path element before using it as a path.- No streaming. The whole body is built / parsed in memory - fine for form posts, not for multi-gigabyte uploads.
- Names / filenames are escaped, not percent-encoded.
buildstrips CR / LF and backslash-escapes\and"inside the quoted parameter (andparseundoes\"/\\); RFC 7578 percent-encoding is not supported.
See also
- mime.md - email-style multipart and header encoding.
- http.md / web.md - send an upload, or handle one.
- modules/index.md - the module catalog and import rules.
API reference
Every exported function, struct and constant of multipart, with its signature, generated from the module's doc comments: the multipart API reference.