Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

render

v2.1.0
gopkg.in/chi.v2/render

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

37 matches

Top ↑

Constants

ContentTypeUnknown (7 declarations)

const (
	ContentTypeUnknown = iota
	ContentTypePlainText
	ContentTypeHTML
	ContentTypeJSON
	ContentTypeXML
	ContentTypeForm
	ContentTypeEventStream
)

ContentTypes handled by this package.

Variables

ContentTypeCtxKey

var (
	ContentTypeCtxKey = &contextKey{"ContentType"}
)

Decode

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.

Respond

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.

StatusCtxKey

var StatusCtxKey = &contextKey{"Status"}

StatusCtxKey is a context key to record a future HTTP response status code.

Functions

Bind

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

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

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.

DecodeJSON

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

DecodeXML

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

DefaultDecoder

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

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.

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.

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.

NoContent

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

NoContent returns a HTTP 204 "No Content" response.

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.

Render

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

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

RenderList

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

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

SetContentType

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

SetContentType is a middleware that forces response Content-Type.

Status

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

XML

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

Types

Binder

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

Binder interface for managing request payloads.

ContentType

type ContentType int

ContentType is an enumeration of common HTTP content types.

GetAcceptedContentType

func GetAcceptedContentType(r *http.Request) ContentType

GetContentType

func GetContentType(s string) ContentType

GetRequestContentType

func GetRequestContentType(r *http.Request) ContentType

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

M

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.

Renderer

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

Renderer interface for managing response payloads.

Dependency graph