Documentation
¶
Overview ¶
Package contenttype implements HTTP Content-Type and Accept header parsers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidMediaType is returned when the media type in the Content-Type or Accept header is syntactically invalid. ErrInvalidMediaType = errors.New("invalid media type") // ErrInvalidMediaRange is returned when the range of media types in the Content-Type or Accept header is syntactically invalid. ErrInvalidMediaRange = errors.New("invalid media range") // ErrInvalidParameter is returned when the media type parameter in the Content-Type or Accept header is syntactically invalid. ErrInvalidParameter = errors.New("invalid parameter") // ErrInvalidExtensionParameter is returned when the media type extension parameter in the Content-Type or Accept header is syntactically invalid. ErrInvalidExtensionParameter = errors.New("invalid extension parameter") // ErrNoAcceptableTypeFound is returned when Accept header contains only media types that are not in the acceptable media type list. ErrNoAcceptableTypeFound = errors.New("no acceptable type found") // ErrNoAvailableTypeGiven is returned when the acceptable media type list is empty. ErrNoAvailableTypeGiven = errors.New("no available type given") // ErrInvalidWeight is returned when the media type weight in Accept header is syntactically invalid. ErrInvalidWeight = errors.New("invalid weight") )
Functions ¶
func GetAcceptableMediaType ¶
func GetAcceptableMediaType(request *http.Request, availableMediaTypes []MediaType) (MediaType, Parameters, error)
GetAcceptableMediaType chooses a media type from available media types according to the Accept. Returns the most suitable media type or an error if no type can be selected.
func GetAcceptableMediaTypeFromHeader ¶ added in v1.0.1
func GetAcceptableMediaTypeFromHeader(headerValue string, availableMediaTypes []MediaType) (MediaType, Parameters, error)
GetAcceptableMediaTypeFromHeader chooses a media type from available media types according to the specified Accept header value. Returns the most suitable media type or an error if no type can be selected.
Types ¶
type MediaType ¶
type MediaType struct {
Type string
Subtype string
Parameters Parameters
}
MediaType holds the type, subtype and parameters of a media type.
func GetMediaType ¶
GetMediaType gets the content of Content-Type header, parses it, and returns the parsed MediaType. If the request does not contain the Content-Type header, an empty MediaType is returned.
func NewMediaType ¶
NewMediaType parses the string and returns an instance of MediaType struct.
func ParseMediaType ¶ added in v1.0.1
ParseMediaType parses the given string as a MIME media type (with optional parameters) and returns it as a MediaType. If the string cannot be parsed an appropriate error is returned.
func (MediaType) MIME ¶ added in v1.0.3
MIME returns the MIME type without any of the parameters
Example ¶
ExampleMediaType_MIME comparing to MIME types
package main
import (
"fmt"
"github.com/elnormous/contenttype"
)
func main() {
mt := contenttype.NewMediaType("application/json; charset=utf-8")
fmt.Printf("MIME(): %s\n", mt.MIME())
fmt.Printf("matches: application/json: %v\n", mt.MIME() == "application/json")
fmt.Printf("matches: application/*: %v\n", mt.MIME() == "application/*")
fmt.Printf("matches: text/plain: %v\n", mt.MIME() == "text/plain")
}
Output: MIME(): application/json matches: application/json: true matches: application/*: false matches: text/plain: false
type Parameters ¶
Parameters represents media type parameters as a key-value map.