contenttype

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2021 License: Unlicense Imports: 3 Imported by: 33

README

Content-Type support library for Go

This library can be used to parse the value Content-Type header (if one is present) and select an acceptable media type from the Accept header of HTTP request.

Usage

Media types are stored in MediaType structure which has Type (e.g. application), Subtype (e.g. json) and Parameters (e.g. charset: utf-8) attributes. Media types are not stored in a string because media type parameters are part of the media type (RFC 7231, 3.1.1.1. Media Type). To convert a string to MediaType use NewMediaType. To convert MediaType back to string use String function. If the Content-Type header is not present in the request, an empty MediaType is returned.

To get the MediaType of the incoming request call GetMediaType and pass the http.Request pointer to it. The function will return error if the Content-Type header is malformed according to RFC 7231, 3.1.1.5. Content-Type.

To get an acceptable media type from an Accept header of the incoming request call GetAcceptableMediaType and pass the http.Request pointer to it and an array of all the acceptable media types. The function will return the best match following the negotiation rules written in RFC 7231, 5.3.2. Accept or an error if the header is malformed or the content type in the Accept header is not supported. If the Accept header is not present in the request, the first media type from the acceptable type list is returned.

import (
	"log"
	"github.com/elnormous/contenttype"
)

func handleRequest(responseWriter http.ResponseWriter, request *http.Request) {
    mediaType, mediaTypeError := contenttype.GetMediaType(request)
    if mediaTypeError != nil {
        // handle the error
    }
    log.Println("Media type:", mediaType.String())

    availableMediaTypes := []MediaType{
        contenttype.NewMediaType("application/json"),
        contenttype.NewMediaType("application/xml"),
    }

    accepted, extParameters, acceptError := contenttype.GetAcceptableMediaType(request, availableMediaTypes)
    if acceptError != nil {
        // handle the error
    }
    log.Println("Accepted media type:", accepted.String(), "extension parameters:", extParameters)
}

Documentation

Overview

Package implement HTTP Content-Type and Accept header parsers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Media type in the Content-Type or Accept header is syntactically invalid.
	ErrInvalidMediaType = errors.New("Invalid media type")
	// Range of media types in the Content-Type or Accept header is syntactically invalid.
	ErrInvalidMediaRange = errors.New("Invalid media range")
	// Media type parameter in the Content-Type or Accept header is syntactically invalid.
	ErrInvalidParameter = errors.New("Invalid parameter")
	// Media type extension parameter in the Content-Type or Accept header is syntactically invalid.
	ErrInvalidExtensionParameter = errors.New("Invalid extension parameter")
	// Accept header contains only media types that are not in the acceptable media type list.
	ErrNoAcceptableTypeFound = errors.New("No acceptable type found")
	// Acceptbale media type list is empty.
	ErrNoAvailableTypeGiven = errors.New("No available type given")
	// Media type weight in the Accept header is syntactically invalid.
	ErrInvalidWeight = errors.New("Invalid wieght")
)

Functions

func GetAcceptableMediaType

func GetAcceptableMediaType(request *http.Request, availableMediaTypes []MediaType) (MediaType, Parameters, error)

Choses 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.

Types

type MediaType

type MediaType struct {
	Type       string
	Subtype    string
	Parameters Parameters
}

A struct for media type which holds type, subtype and parameters.

func GetMediaType

func GetMediaType(request *http.Request) (MediaType, error)

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

func NewMediaType(s string) MediaType

Parses the string and returns an instance of MediaType struct.

func (*MediaType) String

func (mediaType *MediaType) String() string

Converts the MediaType to string.

type Parameters

type Parameters = map[string]string

A map for media type parameters.

Jump to

Keyboard shortcuts

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