response

package
v0.0.0-...-ed9fe1d Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentType string = "content-type"
)

Widely used headers

Variables

View Source
var NoContent = &noContent{}
View Source
var UseEmptyIfSliceIsZero = true

UseEmptyIfSliceIsZero is a configuration variable about if the data is an empty slice ([]T{}), then render [] instead of null (Go's default JSON behavior)

View Source
var UseFormattedJSON = false

UseFormattedJSON is a configuration variable about if json object is formatted or not.

Functions

This section is empty.

Types

type Body

type Body interface {
	Render(ctx context.Context, w io.Writer)
}

Body is an interface to write response

type HTTPStatus

type HTTPStatus int

HTTPStatus is an enum for http status code

const (
	HTTPStatusOK HTTPStatus = 200 + iota
	HTTPStatusCreated
	HTTPStatusAccepted
	HTTPStatusNonAuthoritativeInformation
	HTTPStatusNoContent
	HTTPStatusResetContent
	HTTPStatusPartialContent
)

Available status code for 2xx

const (
	HTTPStatusMovedParmanently HTTPStatus = 301 + iota
	HTTPStatusFound
	HTTPStatusSeeOther
	HTTPStatusNotModified
	HTTPStatusUseProxy
)

Available status code for 3xx

const (
	HTTPStatusBadRequest HTTPStatus = 400 + iota
	HTTPStatusUnauthorized
	HTTPStatusPaymentRequired
	HTTPStatusForbidden
	HTTPStatusNotFound
	HTTPStatusMethodNotAllowed
	HTTPStatusNotAcceptable
	HTTPStatusProxyAuthenticationRequired
	HTTPStatusRequestTimeout
	HTTPStatusCoflict
	HTTPStatusGone
	HTTPStatusLengthRequired
	HTTPStatusPreconditionFailed
	HTTPStatusPayloadTooLarge
	HTTPStatusURITooLong
	HTTPStatusRangeNotSatisfiable
	HTTPStatusExpectationFailed
)

Available status code for 4xx

const (
	HTTPStatusInternalServerError HTTPStatus = 500 + iota
	HTTPStatusNotImplemented
	HTTPStatusBadGateway
	HTTPStatusServiceUnavailable
	HTTPStatusGatewayTimeout
)

Available status code for 5xx

func MustParseHTTPStatus

func MustParseHTTPStatus(s string) HTTPStatus

func ParseHTTPStatus

func ParseHTTPStatus(s string) (HTTPStatus, error)

func ParseHTTPStatusOr

func ParseHTTPStatusOr(s string, or HTTPStatus) HTTPStatus

func (HTTPStatus) MarshalJSON

func (i HTTPStatus) MarshalJSON() ([]byte, error)

func (HTTPStatus) String

func (i HTTPStatus) String() string

func (*HTTPStatus) UnmarshalJSON

func (i *HTTPStatus) UnmarshalJSON(b []byte) error

type Response

type Response struct {
	Status  HTTPStatus
	Header  http.Header
	Cookies []*http.Cookie
	Body    Body
}

Response represents http response.

func NewError

func NewError(e error) *Response

NewError returns a text response

func NewErrorWithStatus

func NewErrorWithStatus(e error, code HTTPStatus) *Response

NewErrorWithStatus returns an error response with the given status code

func NewHTML

func NewHTML(template *template.Template, data interface{}) *Response

NewHTML returns a new *HTML

func NewHTMLWithStatus

func NewHTMLWithStatus(template *template.Template, data interface{}, code HTTPStatus) *Response

NewHTMLWithStatus returns a html response generated by template and data with status code

func NewJSON

func NewJSON(v interface{}) *Response

NewJSON returns a JSON response

func NewJSONWithStatus

func NewJSONWithStatus(v interface{}, code HTTPStatus) *Response

NewJSONWithStatus returns a JSON formatted response with the given status code

func NewRedirect

func NewRedirect(url string) *Response

NewRedirect returns *Responose for redirect

func NewRedirectWithStatus

func NewRedirectWithStatus(url string, status HTTPStatus) *Response

NewRedirectWithStatus returns *Responose for redirect

func NewResponse

func NewResponse(body Body) *Response

NewResponse retuurns a *Response to write body content

func NewResponseWithStatus

func NewResponseWithStatus(body Body, status HTTPStatus) *Response

NewResponseWithStatus retuurns a *Response to write body content with a custom status code.

func NewText

func NewText(s interface{}) *Response

NewText returns a text response

func NewTextWithStatus

func NewTextWithStatus(s interface{}, code HTTPStatus) *Response

NewTextWithStatus returns a text formatted response with the given status code

func (*Response) Content

func (r *Response) Content() string

Content returns the rendered result of the response body

func (*Response) Render

func (r *Response) Render(ctx context.Context, w http.ResponseWriter)

Render renders whole http contnet

func (*Response) SetCookie

func (r *Response) SetCookie(c *http.Cookie, hmac *xhmac.Base64)

SetCookie add a cookie on the response

Directories

Path Synopsis
ia

Jump to

Keyboard shortcuts

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