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.
Purpose: See the package summary above. Import: `github.com/aatuh/api-toolkit/v4/negotiation`. Example: See docs/api-reference.md for package example links and docs/cookbook.md for task recipes. Errors: Constructors, parsers, and handlers return or write documented errors according to their signatures; packages with plain data types do not add hidden error channels. Concurrency: Treat configured middleware and helpers as immutable after construction; request and response values remain request-scoped unless a type documents stronger guarantees. Stability: Stable core API under VERSIONING.md and scripts/apicheck.sh. When not to use: Prefer net/http, application-owned types, or narrower helpers when this package contract is not needed.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentTypeAllowed ¶
ContentTypeAllowed reports whether a Content-Type is allowed.
func RequireAccept ¶
RequireAccept returns middleware that enforces response media negotiation.
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 ¶
ParseAccept parses an Accept header and sorts values by client preference.
type MediaType ¶
type MediaType string
MediaType is a normalized HTTP media type.
func Negotiate ¶
Negotiate chooses the best offered media type for an Accept header.
Example ¶
package main
import (
"fmt"
"github.com/aatuh/api-toolkit/v4/negotiation"
)
func main() {
mediaType, ok := negotiation.Negotiate(
"application/json",
[]negotiation.MediaType{"application/json", "application/problem+json"},
)
fmt.Println(mediaType)
fmt.Println(ok)
}
Output: application/json true
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.