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 ¶
- func DecodeForm(dst interface{}, mf *multipart.Form) error
- func DecodeRequest(dst interface{}, r *http.Request, maxMemory int64) error
- type Decoder
- func (d *Decoder) DecodeForm(dst interface{}, mf *multipart.Form) error
- func (d *Decoder) DecodeRequest(dst interface{}, r *http.Request, maxMemory int64) error
- func (d *Decoder) DelimitWith(r rune) *Decoder
- func (d *Decoder) EscapeWith(r rune) *Decoder
- func (d *Decoder) IgnoreCase(ignoreCase bool) *Decoder
- func (d *Decoder) IgnoreUnknownKeys(ignoreUnknown bool) *Decoder
- func (d *Decoder) KeysWith(f func(string) string) *Decoder
- func (d *Decoder) MaxDepth(maxDepth int) *Decoder
- func (d *Decoder) MaxFileSize(maxFileSize int64) *Decoder
- func (d *Decoder) MaxFiles(maxFiles int) *Decoder
- func (d *Decoder) MaxSize(maxSize int) *Decoder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeForm ¶
DecodeForm decodes the already-parsed multipart form mf 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 (*Decoder) DecodeForm ¶
DecodeForm decodes the already-parsed multipart form mf into dst, which must be a non-nil pointer to a struct.
func (*Decoder) DecodeRequest ¶
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 ¶
DelimitWith sets r as the delimiter used for composite value keys and returns the Decoder; it is '.' by default.
func (*Decoder) EscapeWith ¶
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 ¶
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 ¶
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
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 ¶
MaxDepth corresponds to form's Decoder.MaxDepth, bounding the key-path nesting depth the value decoder will parse.
func (*Decoder) MaxFileSize ¶
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.