negotiation

package
v3.1.2 Latest Latest
Warning

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

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

Documentation

Overview

Package negotiation provides stable core HTTP content negotiation helpers.

Use New when a service wants one middleware to enforce both Accept and Content-Type policy, or RequireAccept and RequireContentType for focused checks. ParseAccept, Negotiate, and ContentTypeAllowed expose the same matching behavior for handlers and tests.

The package writes Problem Details 406 and 415 responses and does not inspect request bodies, route metadata, or OpenAPI documents. Keep media-type policy explicit in application routing or route contracts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentTypeAllowed

func ContentTypeAllowed(header string, allowed []MediaType) bool

ContentTypeAllowed reports whether a Content-Type is allowed.

func RequireAccept

func RequireAccept(allowed ...MediaType) func(http.Handler) http.Handler

RequireAccept returns middleware that enforces response media negotiation.

func RequireContentType

func RequireContentType(allowed ...MediaType) func(http.Handler) http.Handler

RequireContentType returns middleware that enforces request Content-Type.

Types

type Accept

type Accept struct {
	MediaType string
	Type      string
	Subtype   string
	Params    map[string]string
	Q         float64
	Order     int
}

Accept is one parsed Accept header item.

func ParseAccept

func ParseAccept(header string) ([]Accept, error)

ParseAccept parses an Accept header and sorts values by client preference.

type Config

type Config struct {
	Accept       []MediaType
	ContentTypes []MediaType
}

Config configures content negotiation middleware.

type MediaType

type MediaType string

MediaType is a normalized HTTP media type.

func Negotiate

func Negotiate(header string, offered []MediaType) (MediaType, bool)

Negotiate chooses the best offered media type for an Accept header.

type Middleware

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

Middleware enforces Accept and Content-Type policies.

func New

func New(config Config) (*Middleware, error)

New constructs content negotiation middleware.

func (*Middleware) Handler

func (m *Middleware) Handler(next http.Handler) http.Handler

Handler wraps next with Accept and Content-Type checks.

func (*Middleware) Middleware

func (m *Middleware) Middleware() func(http.Handler) http.Handler

Middleware returns the standard middleware adapter.

Jump to

Keyboard shortcuts

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