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 = Form

Parsed multipart form: text fields and file parts.

Examples

Multipart.parse req

File

type File = File

One uploaded file part.

Examples

Multipart.file "avatar" form

Error

type Error
= NotMultipart
| ParseFailed String

Why parse failed.

Examples

Err Multipart.NotMultipart

Values

errorToString

errorToString : Error -> String

Human-readable Error.

Examples

Multipart.errorToString err

parse

parse : Request -> Result Error Form

Parse multipart/form-data from the request body.

Examples

Multipart.parse req

field

field : String -> Form -> Maybe String

First text field with this name.

Examples

Multipart.field "title" form

fields

fields : Form -> Dict String String

All text fields as a dict (last value wins on duplicate names).

Examples

Multipart.fields form

file

file : String -> Form -> Maybe File

First file part with this name.

Examples

Multipart.file "avatar" form

files

files : String -> Form -> List File

All file parts with this name (multi-file inputs).

Examples

Multipart.files "photos" form

filename

filename : File -> Maybe String

Client-provided filename, if any.

Examples

Multipart.filename part

mimeType

mimeType : File -> Maybe String

Part Content-Type, if any.

Examples

Multipart.mimeType part

bytes

bytes : File -> Bytes

Materialize the whole part. Prefer stream / lines for large files.

Examples

Multipart.bytes part

stream

stream : Int -> File -> Stream Bytes

Lazily stream the part as fixed-size byte chunks.

Examples

Multipart.stream 65536 part

lines

lines : File -> Stream String

Lazily stream the part as newline-delimited lines (CSV-friendly).

Examples

Multipart.lines part

fileStream

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