render: github.com/go-chi/render Index | Files

package render

import "github.com/go-chi/render"

Index

Package Files

content_type.go decoder.go render.go responder.go

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.

func Bind Uses

func Bind(r *http.Request, v Binder) error

Bind decodes a request body and executes the Binder method of the payload structure.

func Data Uses

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 Uses

func DecodeJSON(r io.Reader, v interface{}) error

func DecodeXML Uses

func DecodeXML(r io.Reader, v interface{}) error

func DefaultDecoder Uses

func DefaultDecoder(r *http.Request, v interface{}) error

func DefaultResponder Uses

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 Uses

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 Uses

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 Uses

func NoContent(w http.ResponseWriter, r *http.Request)

NoContent returns a HTTP 204 "No Content" response.

func PlainText Uses

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 Render Uses

func Render(w http.ResponseWriter, r *http.Request, v Renderer) error

Render renders a single payload and respond to the client request.

func RenderList Uses

func RenderList(w http.ResponseWriter, r *http.Request, l []Renderer) error

RenderList renders a slice of payloads and responds to the client request.

func SetContentType Uses

func SetContentType(contentType ContentType) func(next http.Handler) http.Handler

SetContentType is a middleware that forces response Content-Type.

func Status Uses

func Status(r *http.Request, status int)

Status sets a HTTP response status code hint into request context at any point during the request life-cycle. Before the Responder sends its response header it will check the StatusCtxKey

func XML Uses

func XML(w http.ResponseWriter, r *http.Request, v interface{})

XML marshals 'v' to JSON, setting the Content-Type as application/xml. It will automatically prepend a generic XML header (see encoding/xml.Header) if one is not found in the first 100 bytes of 'v'.

type Binder Uses

type Binder interface {
    Bind(r *http.Request) error
}

Binder interface for managing request payloads.

type ContentType Uses

type ContentType int

ContentType is an enumeration of common HTTP content types.

func GetAcceptedContentType Uses

func GetAcceptedContentType(r *http.Request) ContentType

func GetContentType Uses

func GetContentType(s string) ContentType

func GetRequestContentType Uses

func GetRequestContentType(r *http.Request) ContentType

GetRequestContentType is a helper function that returns ContentType based on context or request headers.

type M Uses

type M map[string]interface{}

M is a convenience alias for quickly building a map structure that is going out to a responder. Just a short-hand.

type Renderer Uses

type Renderer interface {
    Render(w http.ResponseWriter, r *http.Request) error
}

Renderer interface for managing response payloads.

Package render imports 11 packages (graph) and is imported by 110 packages. Updated 2019-05-05. Refresh now. Tools for package owners.