Help us shape the future of Go! Take the 2026 Go Developer Survey

formdata

package
v1.149.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package formdata assembles a multipart/form-data request body from an object of form fields (#1296): the encoder the api gateway sends an operation that takes form data through, with the boundary generated here and authoritative over any Content-Type a caller supplied. It knows nothing about connections or operations, only the value-shape rules a body object follows.

Index

Constants

View Source
const MediaType = "multipart/form-data"

MediaType is the media type this encoder answers for. An operation whose catalog entry declares it (or a caller who pins it in headers) gets a body assembled by mime/multipart rather than json.Marshal (issue #1296).

Variables

This section is empty.

Functions

func Encode

func Encode(body any) (data []byte, contentType string, err error)

Encode assembles a multipart/form-data body from an object body and returns it with the Content-Type carrying the generated boundary.

The body must be a JSON object; each key is one form field:

  • a scalar becomes a plain text field,
  • an array becomes one part per element under the same field name,
  • an object carrying any of filename / content / content_base64 / content_type becomes a part with those attributes: content is sent as UTF-8 text, content_base64 is decoded first, and a part naming a filename defaults to application/octet-stream. Such an object may carry no other key, so a typo is refused rather than dropped,
  • any other object is JSON-encoded into a text field, which is how upstreams that take a JSON metadata field alongside a file part expect it.

The returned encoding is authoritative: the boundary is generated here and a caller-supplied Content-Type header must not survive, because a boundary that does not match these bytes yields a body the upstream parses as zero parts (issue #1296).

func Is

func Is(contentType string) bool

Is reports whether contentType names multipart/form-data, ignoring any parameters (a boundary) and casing. An unparseable value falls back to comparing the text before the first ";" so a caller's malformed header still routes to the multipart encoder rather than being silently JSON-encoded under a multipart header.

Types

This section is empty.

Jump to

Keyboard shortcuts

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