Documentation ¶
Index ¶
- Constants
- Variables
- func Bind(r *http.Request, v Binder) error
- func Data(w http.ResponseWriter, r *http.Request, v []byte)
- func DecodeJSON(r io.Reader, v interface{}) error
- func DecodeXML(r io.Reader, v interface{}) error
- func DefaultDecoder(r *http.Request, v interface{}) error
- func DefaultResponder(w http.ResponseWriter, r *http.Request, v interface{})
- func HTML(w http.ResponseWriter, r *http.Request, v string)
- func JSON(w http.ResponseWriter, r *http.Request, v interface{})
- func NoContent(w http.ResponseWriter, r *http.Request)
- func PlainText(w http.ResponseWriter, r *http.Request, v string)
- func Render(w http.ResponseWriter, r *http.Request, v Renderer) error
- func RenderList(w http.ResponseWriter, r *http.Request, l []Renderer) error
- func SetContentType(contentType ContentType) func(next http.Handler) http.Handler
- func Status(r *http.Request, status int)
- func XML(w http.ResponseWriter, r *http.Request, v interface{})
- type Binder
- type ContentType
- type M
- type Renderer
Constants ¶
const ( ContentTypeUnknown = iota ContentTypePlainText ContentTypeHTML ContentTypeJSON ContentTypeXML ContentTypeForm ContentTypeEventStream )
ContentTypes handled by this package.
Variables ¶
var (
ContentTypeCtxKey = &contextKey{"ContentType"}
)
var Decode = DefaultDecoder
Decode is a package-level variable set to our default Decoder. We do this because it allows you to set render.Decode to another function with the same function signature, while also utilizing the render.Decoder() function itself. Effectively, allowing you to easily add your own logic to the package defaults. For example, maybe you want to impose a limit on the number of bytes allowed to be read from the request body.
var Respond = DefaultResponder
Respond is a package-level variable set to our default Responder. We do this because it allows you to set render.Respond to another function with the same function signature, while also utilizing the render.Responder() function itself. Effectively, allowing you to easily add your own logic to the package defaults. For example, maybe you want to test if v is an error and respond differently, or log something before you respond.
var StatusCtxKey = &contextKey{"Status"}
StatusCtxKey is a context key to record a future HTTP response status code.
Functions ¶
func Data ¶
func Data(w http.ResponseWriter, r *http.Request, v []byte)
Data writes raw bytes to the response, setting the Content-Type as application/octet-stream.
func DecodeJSON ¶
func DefaultDecoder ¶
func DefaultResponder ¶
func DefaultResponder(w http.ResponseWriter, r *http.Request, v interface{})
Respond handles streaming JSON and XML responses, automatically setting the Content-Type based on request headers. It will default to a JSON response.
func HTML ¶
func HTML(w http.ResponseWriter, r *http.Request, v string)
HTML writes a string to the response, setting the Content-Type as text/html.
func JSON ¶
func JSON(w http.ResponseWriter, r *http.Request, v interface{})
JSON marshals 'v' to JSON, automatically escaping HTML and setting the Content-Type as application/json.
func NoContent ¶
func NoContent(w http.ResponseWriter, r *http.Request)
NoContent returns a HTTP 204 "No Content" response.
func PlainText ¶
func PlainText(w http.ResponseWriter, r *http.Request, v string)
PlainText writes a string to the response, setting the Content-Type as text/plain.
func RenderList ¶
RenderList renders a slice of payloads and responds to the client request.
func SetContentType ¶
func SetContentType(contentType ContentType) func(next http.Handler) http.Handler
SetContentType is a middleware that forces response Content-Type.
Types ¶
type ContentType ¶
type ContentType int
ContentType is an enumeration of common HTTP content types.
func GetAcceptedContentType ¶
func GetAcceptedContentType(r *http.Request) ContentType
func GetContentType ¶
func GetContentType(s string) ContentType
func GetRequestContentType ¶
func GetRequestContentType(r *http.Request) ContentType
GetRequestContentType is a helper function that returns ContentType based on context or request headers.