Skip to content
Jennifer Programming Language

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

NameWhat it is
buildBuild a form body with a fresh random boundary.
buildWithBuild a form body with an explicit boundary (deterministic).
fieldA plain text form field.
fileA file part.
isFileReport whether a part is a file (has a filename).
parseParse a multipart/form-data body into its parts.
textDecode a part's body as a UTF-8 string (for reading text field values).
Builtstruct: A built form body plus the matching Content-Type header (with the boundary).
Partstruct: One form part: a field or a file.

Functions

build

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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-data with a name); kind "limit" past 1000 parts, a part header over 16 KiB, or a name / filename over 1 KiB

text

jennifer
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).

jennifer
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.

jennifer
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