content

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 25 Imported by: 0

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

Constants

This section is empty.

Variables

DisplayRanges includes all viewable Unicode characters along with white space.

Functions

func DisplayableText

func DisplayableText(body []byte) ([]byte, bool)

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.

func Printable

func Printable(body []byte) ([]byte, bool)

Printable returns body when it is likely safe and useful to display as text while sniffing an otherwise unknown response. The cap keeps unknown binary data from being treated as a large text document just because its leading bytes happen to be displayable.

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

type MultipartBody struct {
	Value        any
	ContentTypes map[string]string
}

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 New

func New() *Registry

New returns a Registry with no registrations.

func (*Registry) AcceptEncodingHeader

func (r *Registry) AcceptEncodingHeader() string

AcceptEncodingHeader returns a sorted Accept-Encoding header value built from all registered encodings, ordered by quality descending.

func (*Registry) AcceptHeader

func (r *Registry) AcceptHeader() string

AcceptHeader returns a sorted Accept header value built from all registered content types, ordered by quality descending.

func (*Registry) AcceptHeaderFor

func (r *Registry) AcceptHeaderFor(mediaTypes []string) string

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

func (r *Registry) AddEncoding(e *Encoding)

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

func (r *Registry) Decode(mimeType string, data []byte) (any, error)

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

func (r *Registry) Decompress(encoding string, reader io.Reader) (io.ReadCloser, error)

Decompress wraps r with a decompressor for the named encoding. Returns r unchanged if encoding is empty or "identity".

func (*Registry) Encode

func (r *Registry) Encode(mimeType string, v any) ([]byte, error)

Encode marshals v using the content type matching mimeType. Returns an error if no matching content type is registered.

func (*Registry) EncodeWithType

func (r *Registry) EncodeWithType(mimeType string, v any) ([]byte, string, error)

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

func (r *Registry) MIMETypeForName(name string) string

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.

Jump to

Keyboard shortcuts

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