response

package
v0.0.0-...-9c153ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package response provides methods to send HTTP responses in different formats.

Index

Constants

This section is empty.

Variables

View Source
var DevMode = false

DevMode can be set to true to enable error responses for development/debugging. Otherwise, errors are discarded.

Functions

func Error

func Error(w http.ResponseWriter, r *http.Request, err error, opts ...Option) 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

func Forbidden(w http.ResponseWriter, r *http.Request, opts ...Option) error

Forbidden sends a plain text response with status code 403 (http.StatusForbidden). opts may customize headers.

func JSON

func JSON(w http.ResponseWriter, r *http.Request, payload any, opts ...Option) error

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

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

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

func NotFound(w http.ResponseWriter, r *http.Request, opts ...Option) error

NotFound sends a plain text response with status code 404 (http.StatusNotFound). opts may customize headers.

func NotModified

func NotModified(w http.ResponseWriter, r *http.Request, opts ...Option) error

NotModified sends an empty response with status 304 (http.StatusNotModified). The status code may be changed.

func PlainText

func PlainText(w http.ResponseWriter, r *http.Request, body string, opts ...Option) error

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.

func Send

func Send(w http.ResponseWriter, r *http.Request, opts ...Option) error

Send is a generic response sending operation. It applies all opts and writes the response to w. It returns any technical (i.e. i/o) error that occured. Usually, these errors should be logged but need no further treatment.

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

func AddHeader(h, v string) Option

AddHeader returns an Option that adds a header h with value v to a response. It uses the http.Header.Add method.

func SetHeader

func SetHeader(h, v string, overwwrite bool) Option

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

func StatusCode(sc int) Option

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.

func WriteBody

func WriteBody(buf []byte) Option

WriteBody returns an Option that writes buf as the reponse's body.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL