# form

> Parse HTML form submissions in Langoost — URL-encoded and multipart bodies, returning fields and uploaded files.

# 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

| Function | Signature | Description |
| --- | --- | --- |
| `parseURLEncoded` | `form.parseURLEncoded(body: string) → object` | Parse an `application/x-www-form-urlencoded` body. `files` is always empty |
| `parseMultipart` | `form.parseMultipart(body: string, contentType: string) → object` | Parse a `multipart/form-data` body. Pass the full request `Content-Type` header (it carries the boundary) |

## Example

```goost
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)
}
```