Documentation ¶
Overview ¶
Package content provides content negotiation handlers for the ozzo routing package.
Index ¶
Constants ¶
const ( JSON = routing.MIME_JSON XML = routing.MIME_XML XML2 = routing.MIME_XML2 HTML = routing.MIME_HTML )
MIME types
const Language = "Language"
Language is the key used to store and retrieve the chosen language in routing.Context
Variables ¶
var DataWriters = map[string]routing.DataWriter{ JSON: &JSONDataWriter{}, XML: &XMLDataWriter{}, XML2: &XMLDataWriter{}, HTML: &HTMLDataWriter{}, }
DataWriters lists all supported content types and the corresponding data writers. By default, JSON, XML, and HTML are supported. You may modify this variable before calling TypeNegotiator to customize supported data writers.
Functions ¶
func LanguageNegotiator ¶
func LanguageNegotiator(languages ...string) routing.Handler
LanguageNegotiator returns a content language negotiation handler.
The method takes a list of languages (locale IDs) that are supported by the application. The negotiator will determine the best language to use by checking the Accept-Language request header. If no match is found, the first language will be used.
In a handler, you can access the chosen language through routing.Context like the following:
func(c *routing.Context) error { language := c.Get(content.Language).(string) }
If you do not specify languages, the negotiator will set the language to be "en-US".
func NegotiateContentType ¶
NegotiateContentType negotiates the content types based on the given request and allowed types.
func TypeNegotiator ¶
TypeNegotiator returns a content type negotiation handler.
The method takes a list of response MIME types that are supported by the application. The negotiator will determine the best response MIME type to use by checking the "Accept" HTTP header. If no match is found, the first MIME type will be used.
The negotiator will set the "Content-Type" response header as the chosen MIME type. It will call routing.Context.SetDataWriter() to set the appropriate data writer that can write data in the negotiated format.
If you do not specify any supported MIME types, the negotiator will use "text/html" as the response MIME type.
Types ¶
type AcceptRange ¶
type AcceptRange struct { Type string Subtype string Weight float64 Parameters map[string]string // contains filtered or unexported fields }
AcceptRange represents an accept range as defined in https://tools.ietf.org/html/rfc7231#section-5.3.2
Accept = #( media-range [ accept-params ] )
media-range = ( "*/*" / ( type "/" "*" ) / ( type "/" subtype ) ) *( OWS ";" OWS parameter ) accept-params = weight *( accept-ext ) accept-ext = OWS ";" OWS token [ "=" ( token / quoted-string ) ]
func AcceptMediaTypes ¶
func AcceptMediaTypes(r *http.Request) []AcceptRange
AcceptMediaTypes builds a list of AcceptRange from the given HTTP request.
func ParseAcceptRange ¶
func ParseAcceptRange(accept string) AcceptRange
ParseAcceptRange parses a single accept string into an AcceptRange.
func ParseAcceptRanges ¶
func ParseAcceptRanges(accepts string) []AcceptRange
ParseAcceptRanges parses an Accept header into a list of AcceptRange
func (AcceptRange) RawString ¶
func (a AcceptRange) RawString() string
RawString returns the raw string in the request specifying the accept range.
type HTMLDataWriter ¶
type HTMLDataWriter struct{}
HTMLDataWriter sets the "Content-Type" response header as "text/html; charset=UTF-8" and calls routing.DefaultDataWriter to write the given data to the response.
func (*HTMLDataWriter) SetHeader ¶
func (w *HTMLDataWriter) SetHeader(res http.ResponseWriter)
SetHeader sets the Content-Type response header.
func (*HTMLDataWriter) Write ¶
func (w *HTMLDataWriter) Write(res http.ResponseWriter, data interface{}) error
type JSONDataWriter ¶
type JSONDataWriter struct{}
JSONDataWriter sets the "Content-Type" response header as "application/json" and writes the given data in JSON format to the response.
func (*JSONDataWriter) SetHeader ¶
func (w *JSONDataWriter) SetHeader(res http.ResponseWriter)
SetHeader sets the Content-Type response header.
func (*JSONDataWriter) Write ¶
func (w *JSONDataWriter) Write(res http.ResponseWriter, data interface{}) (err error)
type XMLDataWriter ¶
type XMLDataWriter struct{}
XMLDataWriter sets the "Content-Type" response header as "application/xml; charset=UTF-8" and writes the given data in XML format to the response.
func (*XMLDataWriter) SetHeader ¶
func (w *XMLDataWriter) SetHeader(res http.ResponseWriter)
SetHeader sets the Content-Type response header.
func (*XMLDataWriter) Write ¶
func (w *XMLDataWriter) Write(res http.ResponseWriter, data interface{}) (err error)