multipartkit/encoder

Values

pub fn encode(
  boundary: String,
  parts: List(part.Part),
) -> Result(BitArray, error.MultipartError)

Encode parts using the supplied boundary.

boundary is validated against RFC 2046 §5.1.1 before any bytes are produced; a value that contains CR / LF / NUL / other non-bchars, is empty, or exceeds 70 chars surfaces as Error(InvalidBoundary(value)) so callers cannot accidentally emit a wire image whose framing bytes inject forged headers (the encode-side companion to the header CRLF guard in Part.new/5).

pub fn encode_form(the_form: form.Form) -> #(String, BitArray)

Encode a Form and return #(content_type, body).

content_type is the full value to set on the HTTP Content-Type header — for example multipart/form-data; boundary=----abc123. The boundary is generated freshly per call. Two calls on the same Form therefore produce different content_type values.

Note: v0.1.0 uses gleam/int.random for boundary character generation. This is sufficient for collision avoidance with normal payloads but is not cryptographically strong; do not rely on the boundary for security invariants.

pub fn encode_stream(
  boundary: String,
  parts: yielder.Yielder(stream.StreamPart),
) -> yielder.Yielder(Result(BitArray, error.MultipartError))

Encode a stream of StreamParts into a yielder of byte chunks.

boundary is validated against RFC 2046 §5.1.1 up-front; an invalid boundary makes the returned yielder produce a single Error(InvalidBoundary(value)) and then exhaust, mirroring the encode/2 guard.

Errors propagated from a StreamPart’s body iterator are forwarded as Error(_). After the first error, the yielder is exhausted.

Search Document