content

package
v0.0.0-...-be655d7 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2016 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package content provides content negotiation handlers for the ozzo routing package.

Index

Constants

View Source
const (
	JSON = "application/json"
	XML  = "application/xml"
	HTML = "text/html"
)

MIME types

View Source
const Language = "Language"

Language is the key used to store and retrieve the chosen language in routing.Context

Variables

Formatters lists all supported content types and the corresponding formatters. By default, JSON, XML, and HTML are supported. You may modify this variable before calling TypeNegotiator to customize supported formatters.

Functions

func HTMLFormatter

func HTMLFormatter(res http.ResponseWriter) routing.SerializeFunc

HTMLFormatter sets the "Content-Type" response header as "text/html; charset=UTF-8" and returns a routing.WriteFunc that writes the given data in a byte stream.

func JSONFormatter

func JSONFormatter(res http.ResponseWriter) routing.SerializeFunc

JSONFormatter sets the "Content-Type" response header as "application/json" and returns a routing.WriteFunc that writes the given data in JSON format.

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 TypeNegotiator

func TypeNegotiator(formats ...string) routing.Handler

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 request 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 also set routing.Context.Write to be the function that would serialize the given data in the appropriate format.

If you do not specify any supported MIME types, the negotiator will use "text/html" as the response MIME type.

func XMLFormatter

func XMLFormatter(res http.ResponseWriter) routing.SerializeFunc

XMLFormatter sets the "Content-Type" response header as "application/xml; charset=UTF-8" and returns a routing.WriteFunc that writes the given data in XML format.

Types

type Formatter

type Formatter func(http.ResponseWriter) routing.SerializeFunc

Formatter is a function setting response content type and returning a routing.SerializeFunc for writing data.

Jump to

Keyboard shortcuts

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