render package - github.com/go-chi/render - Go Packages

This section is empty.

View Source

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

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 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 is a context key to record a future HTTP response status code.

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

Data writes raw bytes to the response, setting the Content-Type as application/octet-stream.

DecodeForm decodes a given reader into an interface using the form decoder.

DecodeJSON decodes a given reader into an interface using the json decoder.

DecodeXML decodes a given reader into an interface using the xml decoder.

DefaultDecoder detects the correct decoder for use on an HTTP request and marshals into a given 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 writes a string to the response, setting the Content-Type as text/html.

JSON marshals 'v' to JSON, automatically escaping HTML and setting the Content-Type as application/json.

func NoContent

NoContent returns a HTTP 204 "No Content" response.

PlainText writes a string to the response, setting the Content-Type as text/plain.

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

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

func SetContentType

SetContentType is a middleware that forces response Content-Type.

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 marshals 'v' to XML, 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'.

Binder interface for managing request payloads.

type ContentType

ContentType is an enumeration of common HTTP content types.

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

ContentTypes handled by this package.

func GetRequestContentType

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

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

Renderer interface for managing response payloads.