Multipart
Config & IO
Parse multipart/form-data request bodies (browser form uploads).
parse materializes the request body (memory or spool) into a Form. Text fields are strings; file parts can be folded with lines / stream.
For large uploads, prefer streaming a named file part straight into another consumer (for example Csv.decode) without building a Form:
case Multipart.fileStream "csv" req of
Ok bytes ->
bytes
|> Csv.decode Csv.rfc4180
|> Stream.stoppableFoldl validateRow (Ok 0)
Err err ->
Server.string 400 [] "text/plain" (Multipart.errorToString err)Body streams are single-use and must be consumed inside handle. Large uploads still need a high enough Server.maxBodySize. Eager parse part temp files are deleted when the request finishes (same cleanup as the body spool).
Types
Form
type Form = FormParsed multipart form: text fields and file parts.
Examples
Multipart.parse reqFile
type File = FileOne uploaded file part.
Examples
Multipart.file "avatar" formError
type Error
= NotMultipart
| ParseFailed StringWhy parse failed.
Examples
Err Multipart.NotMultipartValues
errorToString
errorToString : Error -> StringHuman-readable Error.
Examples
Multipart.errorToString errparse
parse : Request -> Result Error FormParse multipart/form-data from the request body.
Examples
Multipart.parse reqfield
field : String -> Form -> Maybe StringFirst text field with this name.
Examples
Multipart.field "title" formfields
fields : Form -> Dict String StringAll text fields as a dict (last value wins on duplicate names).
Examples
Multipart.fields formfile
file : String -> Form -> Maybe FileFirst file part with this name.
Examples
Multipart.file "avatar" formfiles
files : String -> Form -> List FileAll file parts with this name (multi-file inputs).
Examples
Multipart.files "photos" formfilename
filename : File -> Maybe StringClient-provided filename, if any.
Examples
Multipart.filename partmimeType
mimeType : File -> Maybe StringPart Content-Type, if any.
Examples
Multipart.mimeType partbytes
bytes : File -> BytesMaterialize the whole part. Prefer stream / lines for large files.
Examples
Multipart.bytes partstream
stream : Int -> File -> Stream BytesLazily stream the part as fixed-size byte chunks.
Examples
Multipart.stream 65536 partlines
lines : File -> Stream StringLazily stream the part as newline-delimited lines (CSV-friendly).
Examples
Multipart.lines partfileStream
fileStream : String -> Request -> Result Error (Stream Bytes)Stream a named file part's bytes from the live request body (socket or spool).
Skips earlier parts. Single-consumer on the request body — do not also call parse / Server.body / bodyStream on the same request.
Examples
Multipart.fileStream "csv" req