Documentation ¶
Overview ¶
Package response provides methods to send HTTP responses in different formats.
Index ¶
- Variables
- func Error(w http.ResponseWriter, r *http.Request, err error, opts ...Option) error
- func Forbidden(w http.ResponseWriter, r *http.Request, opts ...Option) error
- func JSON(w http.ResponseWriter, r *http.Request, payload any, opts ...Option) error
- func NoContent(w http.ResponseWriter, r *http.Request, opts ...Option) error
- func NotFound(w http.ResponseWriter, r *http.Request, opts ...Option) error
- func NotModified(w http.ResponseWriter, r *http.Request, opts ...Option) error
- func PlainText(w http.ResponseWriter, r *http.Request, body string, opts ...Option) error
- func Problem(w http.ResponseWriter, r *http.Request, problemDetails ProblemDetails, ...) error
- func Send(w http.ResponseWriter, r *http.Request, opts ...Option) error
- type Option
- type ProblemDetails
Constants ¶
This section is empty.
Variables ¶
var DevMode = false
DevMode can be set to true to enable error responses for development/debugging. Otherwise, errors are discarded.
Functions ¶
func Error ¶
Error sends an error response. By default, a status code 500 http.StatusInternalServerError is used but opts may replace this with a different status code.
This operation pays respect to DevMode. If DevMode is false (the default), Error simply sends an empty response with the respective status code. If DevMode is set to true, this method sends the errors description as a plain text response simplifying development.
func Forbidden ¶
Forbidden sends a plain text response with status code 403 (http.StatusForbidden). opts may customize headers.
func JSON ¶
JSON sends a response with content-type application/json. It uses json.Marshal to marshal payload to JSON bytes and sends them as the response' body. JSON sets both header content-type and content-length. opts may further customize the response as well as overwrite the content-type (to some other content type based on JSON).
If DevMode is set to true, JSON output is pretty printed
func NoContent ¶
NoContent sends an empty response with status 204 (http.StatusNoContent) by default. The content-length header is set to 0 and cannot be overwritten. opts may changed the status code.
func NotFound ¶
NotFound sends a plain text response with status code 404 (http.StatusNotFound). opts may customize headers.
func NotModified ¶
NotModified sends an empty response with status 304 (http.StatusNotModified). The status code may be changed.
func PlainText ¶
PlainText sends a response with a plain text body. It sends body as the response' body. opts may further customize the response (headers, status code). PlainText sets content-type to text/plain (overwritable) and content-length to the body's length (not overwritable).
func Problem ¶
func Problem(w http.ResponseWriter, r *http.Request, problemDetails ProblemDetails, opts ...Option) error
Problem sends problemDetails as a JSON response as defined by RFC9457.
Types ¶
type Option ¶
type Option func(w http.ResponseWriter, r *http.Request) error
Option defines a function type used to customize a response. Any non-nil error returned from an Option causes the response to be aborted.
func AddHeader ¶
AddHeader returns an Option that adds a header h with value v to a response. It uses the http.Header.Add method.
func SetHeader ¶
SetHeader returns an Option that sets a header h with value v to a response. It uses the http.Header.Set method. overwrite defines whether SetHeader should overwrite a header value for h set previously or if should skip setting v.
func StatusCode ¶
StatusCode is an Option that sets a response' HTTP status code. Only the first StatusCode Option applied to a response takes effect. All following calls are no-ops.
type ProblemDetails ¶
type ProblemDetails struct { // Type discriminator - must be given Type string `json:"type"` // Human readable title - must be given Title string `json:"title"` // Status code - may be set. If set, also defines the HTTP status code Status int `json:"status,omitempty"` // Additional human readable details - optional Detail string `json:"detail,omitempty"` // Identifier pointing to the instance that caused this problem - optional Instance string `json:"instance,omitempty"` // Additional user defined error information - optional and used as an extension Errors []any `json:"errors,omitempty"` }
ProblemDetails defines a problem details object as defined by RFC9457. ProblemDetails defines an Errors field which may be used to deliver additional error information as an extension.