negotiation

package
v2.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package negotiation provides HTTP content negotiation helpers.

It handles Accept and Content-Type checks for APIs that need more than the simple JSON-only middleware.

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