Documentation
¶
Overview ¶
Package content provides a registry of content types and encodings for marshalling/unmarshalling request and response bodies, and for transparent decompression of compressed responses.
Index ¶
- Variables
- func DisplayableText(body []byte) ([]byte, bool)
- func Printable(body []byte) ([]byte, bool)
- type ContentType
- type Encoding
- type MultipartBody
- type Registry
- func (r *Registry) AcceptEncodingHeader() string
- func (r *Registry) AcceptHeader() string
- func (r *Registry) AcceptHeaderFor(mediaTypes []string) string
- func (r *Registry) AddContentType(ct *ContentType)
- func (r *Registry) AddEncoding(e *Encoding)
- func (r *Registry) ContentTypes() []*ContentType
- func (r *Registry) Decode(mimeType string, data []byte) (any, error)
- func (r *Registry) Decompress(encoding string, reader io.Reader) (io.ReadCloser, error)
- func (r *Registry) Encode(mimeType string, v any) ([]byte, error)
- func (r *Registry) EncodeWithType(mimeType string, v any) ([]byte, string, error)
- func (r *Registry) MIMETypeForName(name string) string
Constants ¶
This section is empty.
Variables ¶
var DisplayRanges = []*unicode.RangeTable{ unicode.L, unicode.M, unicode.N, unicode.P, unicode.S, unicode.White_Space, }
DisplayRanges includes all viewable Unicode characters along with white space.
Functions ¶
func DisplayableText ¶
DisplayableText returns body when it is valid UTF-8 and safe to write as terminal text. Unlike Printable, it does not impose a size cap; callers should use it only after the response is already known to be text.
Types ¶
type ContentType ¶
type ContentType struct {
// Name is a short identifier used in CLI flags (e.g. "json", "yaml").
Name string
// MIMETypes lists all MIME types this entry handles (e.g. "application/json").
// A trailing "/*" is treated as a wildcard prefix match.
MIMETypes []string
// Suffixes lists structured syntax suffixes handled by this entry, such as
// "+json" or "+cbor".
Suffixes []string
// Quality is the Accept header q-value (0–1). Higher = preferred.
Quality float32
// Marshal encodes v into bytes.
Marshal func(v any) ([]byte, error)
// MarshalContentType optionally encodes v and returns the concrete
// Content-Type to send. This is useful for formats like multipart where
// runtime parameters (for example a boundary) are required.
MarshalContentType func(v any) ([]byte, string, error)
// Unmarshal decodes data into a Go value.
Unmarshal func(data []byte) (any, error)
}
ContentType describes how to marshal and unmarshal a single MIME type.
type Encoding ¶
type Encoding struct {
// Name is the encoding token used in Accept-Encoding / Content-Encoding.
Name string
// Quality is the Accept-Encoding q-value.
Quality float32
// Decompress wraps r with a decompressing reader.
Decompress func(r io.Reader) (io.ReadCloser, error)
}
Encoding describes how to decompress a single Content-Encoding.
type MultipartBody ¶
MultipartBody carries a structured multipart/form-data body plus optional per-field Content-Type metadata from an OpenAPI encoding object.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds the set of known content types and encodings.
func Default ¶
func Default() *Registry
Default returns a Registry pre-loaded with JSON, YAML, CBOR, msgpack, Ion, plain text, and gzip/deflate/brotli encodings.
func (*Registry) AcceptEncodingHeader ¶
AcceptEncodingHeader returns a sorted Accept-Encoding header value built from all registered encodings, ordered by quality descending.
func (*Registry) AcceptHeader ¶
AcceptHeader returns a sorted Accept header value built from all registered content types, ordered by quality descending.
func (*Registry) AcceptHeaderFor ¶
AcceptHeaderFor returns an Accept header value for the requested media types that the registry can decode, ordered by registered quality descending.
func (*Registry) AddContentType ¶
func (r *Registry) AddContentType(ct *ContentType)
AddContentType registers a content type. Later registrations take precedence for MIME-type matching when two entries share the same type.
func (*Registry) AddEncoding ¶
AddEncoding registers a compression encoding.
func (*Registry) ContentTypes ¶
func (r *Registry) ContentTypes() []*ContentType
ContentTypes returns all registered content types in registration order.
func (*Registry) Decode ¶
Decode finds the best-matching registered content type for mimeType, unmarshals data, and normalizes all map keys to strings so the result is always safe to pass to encoding/json. Returns the raw bytes as a string or []byte if no match is found.
func (*Registry) Decompress ¶
Decompress wraps r with a decompressor for the named encoding. Returns r unchanged if encoding is empty or "identity".
func (*Registry) Encode ¶
Encode marshals v using the content type matching mimeType. Returns an error if no matching content type is registered.
func (*Registry) EncodeWithType ¶
EncodeWithType marshals v using the content type matching mimeType and returns both the encoded bytes and the concrete Content-Type header value.
func (*Registry) MIMETypeForName ¶
MIMETypeForName returns the primary MIME type for the content type registered under the given short name (e.g. "json" → "application/json"). Returns an empty string if no match is found.