negotiate

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package negotiate implements HTTP content negotiation for selecting request and response formats based on Accept and Content-Type headers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFormats

func DefaultFormats() map[string]core.Format

DefaultFormats returns a format map with JSON registered under both "application/json" and "json" content types.

func DefaultJSONFormat

func DefaultJSONFormat() core.Format

DefaultJSONFormat returns a Format that uses the standard library JSON encoder and decoder with HTML escaping disabled.

func SelectQValue

func SelectQValue(header string, allowed []string) string

SelectQValue parses an Accept header and returns the allowed content type with the highest quality value. It returns an empty string if no match is found.

func SelectQValueFast

func SelectQValueFast(header string, allowed []string) string

SelectQValueFast is an optimized version of SelectQValue that avoids allocations by scanning the header in a single pass.

Types

type Negotiator

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

Negotiator selects the appropriate format for marshalling and unmarshalling based on HTTP content type headers.

func NewNegotiator

func NewNegotiator(formats map[string]core.Format, defaultFormat string, noFallback bool) *Negotiator

NewNegotiator creates a Negotiator with the given format map, default content type, and fallback behavior. When noFallback is true, unrecognized Accept values produce an error instead of falling back to the default format.

func (*Negotiator) Marshal

func (n *Negotiator) Marshal(w io.Writer, ct string, v any) error

Marshal encodes the value v into the writer using the format associated with the given content type.

func (*Negotiator) Negotiate

func (n *Negotiator) Negotiate(accept string) (string, error)

Negotiate selects the best content type from the Accept header value, returning the matched content type or an error if no match is found.

func (*Negotiator) Unmarshal

func (n *Negotiator) Unmarshal(ct string, data []byte, v any) error

Unmarshal decodes the byte slice into v using the format associated with the given content type, defaulting to JSON if the content type is empty.

Jump to

Keyboard shortcuts

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