form

The form module parses HTML form request bodies. Both parsers return an object {fields, files}: fields[name] is a string for a single value or a string[] for repeated values, and files[name] is an object {filename, contentType, bytes, size} (or an array of those for repeated file fields). Import it with import form.

Functions

FunctionSignatureDescription
parseURLEncodedform.parseURLEncoded(body: string) → objectParse an application/x-www-form-urlencoded body. files is always empty
parseMultipartform.parseMultipart(body: string, contentType: string) → objectParse a multipart/form-data body. Pass the full request Content-Type header (it carries the boundary)

Example

import form

// req.body and the Content-Type header come from your HTTP handler
let parsed = form.parseMultipart(req.body, contentType)

println(parsed.fields.username)              // a submitted text field

let avatar = parsed.files.avatar
if typeof(avatar) != "void" {
    println(avatar.filename + " (" + toString(avatar.size) + " bytes)")
    io.write("/tmp/" + avatar.filename, avatar.bytes)
}
Standard library · View as Markdown · llms-full.txt