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. Pure .j over strings + bytes; runs on both binaries.
Import with import "multipart.j" as multipart;. See the multipart guide for prose and examples.
Functions
multipart.build(parts as list of Part)
Build a form body with a fresh random boundary.
Parameters
parts{list of Part}- the parts
Returns {Built} - the Content-Type and encoded body
multipart.buildWith(parts as list of Part, boundary as string)
Build a form body with an explicit boundary (deterministic).
Parameters
parts{list of Part}- the partsboundary{string}- the boundary token (must not occur in any part body)
Returns {Built} - the Content-Type and encoded body
multipart.field(name as string, value as string)
A plain text form field.
Parameters
name{string}- the field namevalue{string}- the field value
Returns {Part} - the part
multipart.file(name as string, filename as string, contentType as string, data as bytes)
A file part.
Parameters
name{string}- the field namefilename{string}- the file namecontentType{string}- the file's Content-Type (e.g. "text/plain")data{bytes}- the file content
Returns {Part} - the part
multipart.isFile(p as Part)
Report whether a part is a file (has a filename).
Parameters
p{Part}- the part
Returns {bool} - true if the part carries a filename
multipart.parse(contentType as string, body as bytes)
Parse a multipart/form-data body into its parts.
Parameters
contentType{string}- the Content-Type header (carrying the boundary)body{bytes}- the encoded body
Returns {list of Part} - the parts (fields and files)
Throws
{Error}- kind "multipart" if the boundary is missing or a part is malformed
multipart.text(p as Part)
Decode a part's body as a UTF-8 string (for reading text field values).
Parameters
p{Part}- the part
Returns {string} - the decoded body
Structs
multipart.Built
A built form body plus the matching Content-Type header (with the boundary).
| Field | Type | Description |
|---|---|---|
contentType | string | the multipart/form-data; boundary=... value |
body | bytes | the encoded body |
multipart.Part
One form part: a field or a file.
| Field | Type | Description |
|---|---|---|
name | string | the field name |
filename | string | the file name ("" for a plain field) |
contentType | string | the part's Content-Type ("" for a plain field) |
data | bytes | the part body |