multipart

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package multipart decodes multipart/form-data — as submitted by HTML forms containing file inputs — into Go structs, using github.com/ajg/form to decode the value fields and mapping file parts onto struct fields by name.

Value fields follow form's decoding rules (struct tags, nesting, custom delimiters, and so on). File fields are matched by their top-level part name — via the `form` or `json` struct tag, or the field name — and may be declared as any of:

*multipart.FileHeader   // lazily read: the first file sent under the key
[]*multipart.FileHeader // lazily read: every file sent under the key
[]byte                  // eagerly read: contents of the first file
[][]byte                // eagerly read: contents of every file

The *multipart.FileHeader forms hand over mime/multipart's own handle: the caller Opens (and Closes) the file itself and may stream it, so no bound is imposed here. The []byte forms read the file into memory during decoding, bounded by Decoder.MaxFileSize and Decoder.MaxFiles.

Like form, decoding is strict by default: a value or file whose key matches no destination field is an error. Real browser submissions often include fields that aren't interesting to model (CSRF tokens, submit-button names); either add matching struct fields or opt out of strictness with NewDecoder().IgnoreUnknownKeys(true).

Errors returned by this package are of type *form.Error, carrying Op (form.OpDecode) and Kind (form.KindUnknownKey, form.KindLimit, and so on), so they participate in the same errors.As taxonomy as form itself. Field names can be mapped to wire keys with Decoder.KeysWith, mirroring form.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeForm

func DecodeForm(dst interface{}, mf *multipart.Form) error

DecodeForm decodes the already-parsed multipart form mf into dst using a default (strict, bounded) Decoder.

func DecodeRequest

func DecodeRequest(dst interface{}, r *http.Request, maxMemory int64) error

DecodeRequest parses r's body as multipart/form-data and decodes the result into dst using a default (strict, bounded) Decoder.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder decodes multipart/form-data into a struct. The zero configuration (NewDecoder) is strict and bounded; see the methods for the knobs.

func NewDecoder

func NewDecoder() *Decoder

NewDecoder returns a new multipart Decoder.

func (*Decoder) DecodeForm

func (d *Decoder) DecodeForm(dst interface{}, mf *multipart.Form) error

DecodeForm decodes the already-parsed multipart form mf into dst, which must be a non-nil pointer to a struct.

func (*Decoder) DecodeRequest

func (d *Decoder) DecodeRequest(dst interface{}, r *http.Request, maxMemory int64) error

DecodeRequest parses r's body as multipart/form-data — via (*http.Request).ParseMultipartForm, buffering up to maxMemory bytes of file data in memory and the remainder in temporary files — and decodes the result into dst, which must be a non-nil pointer to a struct.

func (*Decoder) DelimitWith

func (d *Decoder) DelimitWith(r rune) *Decoder

DelimitWith sets r as the delimiter used for composite value keys and returns the Decoder; it is '.' by default.

func (*Decoder) EscapeWith

func (d *Decoder) EscapeWith(r rune) *Decoder

EscapeWith sets r as the escape used for delimiters (and to escape itself) in value keys and returns the Decoder; it is '\\' by default.

func (*Decoder) IgnoreCase

func (d *Decoder) IgnoreCase(ignoreCase bool) *Decoder

IgnoreCase, if set to true, makes the Decoder try to set value fields even if the case of the key does not match. It applies to value fields only; file fields are always matched exactly.

func (*Decoder) IgnoreUnknownKeys

func (d *Decoder) IgnoreUnknownKeys(ignoreUnknown bool) *Decoder

IgnoreUnknownKeys, if set to true, makes the Decoder silently skip values and files whose keys match no destination field, instead of returning an error. It is false by default, matching form; setting it to true is the pragmatic choice for raw browser submissions, which routinely carry fields (CSRF tokens, submit-button names) that the destination doesn't model.

func (*Decoder) KeysWith added in v1.9.0

func (d *Decoder) KeysWith(f func(string) string) *Decoder

KeysWith sets f as a transformation applied to struct field names — for both value fields (via form's Decoder.KeysWith) and file fields — to obtain their form keys, and returns the Decoder. Fields with an explicit tag are exempt: tags always name keys verbatim. Passing nil clears the transformation; see form.Decoder.KeysWith for the full mapper contract.

func (*Decoder) MaxDepth

func (d *Decoder) MaxDepth(maxDepth int) *Decoder

MaxDepth corresponds to form's Decoder.MaxDepth, bounding the key-path nesting depth the value decoder will parse.

func (*Decoder) MaxFileSize

func (d *Decoder) MaxFileSize(maxFileSize int64) *Decoder

MaxFileSize overrides how many bytes of a single file the Decoder will read into memory for a []byte or [][]byte field; a larger file is an error. A value > 0 sets the bound; a value < 0 disables it (trusted input only); the zero value uses the built-in default of 10 MiB. It has no effect on *multipart.FileHeader fields, which the caller reads lazily.

func (*Decoder) MaxFiles

func (d *Decoder) MaxFiles(maxFiles int) *Decoder

MaxFiles overrides how many files the Decoder will accept under a single key for a slice-valued file field; more is an error. A value > 0 sets the bound; a value < 0 disables it; the zero value uses the built-in default of 1000.

func (*Decoder) MaxSize

func (d *Decoder) MaxSize(maxSize int) *Decoder

MaxSize corresponds to form's Decoder.MaxSize, bounding how large a slice the value decoder will grow in response to an explicit index in the input.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL