Documentation
¶
Overview ¶
Package respond provides low-touch idiomatic API responses for Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func With ¶
func With(w http.ResponseWriter, r *http.Request, status int, data interface{})
With responds to the client.
func WithStatus ¶
func WithStatus(w http.ResponseWriter, r *http.Request, status int)
WithStatus responds to the client with the specified status. Options.StatusData will be called to obtain the data payload, or a default payload will be returned:
{"status":"I'm a teapot","code":418}
Types ¶
type Encoder ¶
type Encoder interface {
// Encode writes a serialization of v to w, optionally using additional
// information from the http.Request to do so.
Encode(w http.ResponseWriter, r *http.Request, v interface{}) error
// ContentType gets a string that will become the Content-Type header
// when responding through w to the specified http.Request.
// Most of the time the argument will be ignored, but occasionally
// details in the request, or even in the headers in the ResponseWriter may
// change the content type.
ContentType(w http.ResponseWriter, r *http.Request) string
}
Encoder descirbes an object capable of encoding a response.
type Options ¶
type Options struct {
// AllowMultiple indicates that multiple responses are allowed. Otherwise,
// multiple calls to With will panic.
AllowMultiple bool
// OnErr is a function field that gets called when an
// error occurs while responding.
// By default, the error panic but you may
// use Options.OnErrLog to just log the error out instead,
// or provide your own.
OnErr func(err error)
// Encoder is a function field that gets the encoder to
// use to respond to the specified http.Request.
// If nil, JSON will be used.
Encoder func(w http.ResponseWriter, r *http.Request) Encoder
// Before is called for before each response is written
// and gives user code the chance to mutate the status or data.
// Useful for handling different types of data differently (like errors),
// enveloping the response, setting common HTTP headers etc.
Before func(w http.ResponseWriter, r *http.Request, status int, data interface{}) (int, interface{})
// After is called after each response.
// Useful for logging activity after a response has been written.
After func(w http.ResponseWriter, r *http.Request, status int, data interface{})
// StatusData is a function field that gets the data to respond with when
// WithStatus is called.
// By default, the function will return an object that looks like this:
// {"status":"Not Found","code":404}
StatusData func(w http.ResponseWriter, r *http.Request, status int) interface{}
}
Options provides additional control over the behaviour of With.
Click to show internal directories.
Click to hide internal directories.