Categorygithub.com/go-chi/render
modulepackage
1.0.3
Repository: https://github.com/go-chi/render.git
Documentation: pkg.go.dev

# README

render

tests Go Report Card Go Reference

The render package helps manage HTTP request / response payloads.

Every well-designed, robust and maintainable Web Service / REST API also needs well-defined request and response payloads. Together with the endpoint handlers, the request and response payloads make up the contract between your server and the clients calling on it.

Typically in a REST API application, you will have your data models (objects/structs) that hold lower-level runtime application state, and at times you need to assemble, decorate, hide or transform the representation before responding to a client. That server output (response payload) structure, is also likely the input structure to another handler on the server.

This is where render comes in - offering a few simple helpers and interfaces to provide a simple pattern for managing payload encoding and decoding.

We've also combined it with some helpers for responding to content types and parsing request bodies. Please have a look at the rest example which uses the latest chi/render sub-pkg.

All feedback is welcome, thank you!

# Functions

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.
No description provided by the author
No description provided by the author
GetRequestContentType is a helper function that returns ContentType based on context or request headers.
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.
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.
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.
XML marshals 'v' to XML, setting the Content-Type as application/xml.

# Constants

ContentTypes handled by this package.
ContentTypes handled by this package.
ContentTypes handled by this package.
ContentTypes handled by this package.
ContentTypes handled by this package.
ContentTypes handled by this package.
ContentTypes handled by this package.

# Variables

No description provided by the author
Decode is a package-level variable set to our default Decoder.
Respond is a package-level variable set to our default Responder.
StatusCtxKey is a context key to record a future HTTP response status code.

# Interfaces

Binder interface for managing request payloads.
Renderer interface for managing response payloads.

# Type aliases

ContentType is an enumeration of common HTTP content types.
M is a convenience alias for quickly building a map structure that is going out to a responder.