Documentation ¶
Overview ¶
Response helper interface for structured json responses. Response has also support to provide HTML content, but primarily it's
targeted to json rest apis
Response supports errors, raw content, etc..
Setter methods support chaining so writing response to http is doable on single line In next examples we use these variables
w http.ResponseWriter r *http.Request
Example of responses
response.New(http.StatusInternalServerError).Error(errors.New("error")).Write(w, r) response.New().Error(structError).Write(w, r) response.New().Result(product).Write(w, r) response.New().Result(products).ResultSize(size).Write(w, r) response.New().SliceResult(products).Write(w, r) response.New(http.StatusForbidden).Write(w, r)
Also there is non required argument status
body := map[string]string{ "version": "1.0beta" } response.New(http.StatusOK).Body(body)Write(w, r) response.New(http.StatusOK).Result(product).Write(w, r) response.New(http.StatusOK).SliceResult(products).Write(w, r)
Minimal support for html responses
response.New().HTML("<html></html>").Write(w, r)
Index ¶
- Constants
- Variables
- func Format(f KeyFormat)
- func GetErrorStatus(err error) int
- func RegisterError(err error, status int)
- func Write(w http.ResponseWriter, r *http.Request)
- type KeyFormat
- type Response
- func BadRequest() Response
- func Body(body interface{}) Response
- func Data(key string, value interface{}) Response
- func Error(err interface{}) Response
- func HTML(html string) Response
- func New(statuses ...int) (result Response)
- func NotFound() Response
- func OK() Response
- func Result(result interface{}) Response
- func SliceResult(result interface{}) Response
- func Unauthorized() Response
Constants ¶
const STATUS_HEADER = "X-Response-Status"
Variables ¶
var ( // CamelCaseFormat sets common keys as camel case CamelCaseFormat = KeyFormat{ ErrorKey: "Error", MessageKey: "Message", ResultKey: "Result", ResultSizeKey: "ResultSize", StatusKey: "Status", } // SnakeCaseFormat sets common keys as snake case SnakeCaseFormat = KeyFormat{ ErrorKey: "error", MessageKey: "message", ResultKey: "result", ResultSizeKey: "result_size", StatusKey: "status", } )
Functions ¶
func GetErrorStatus ¶
GetErrorStatus returns appropriate http status for given error
func RegisterError ¶
RegisterError registers error to given http status
Types ¶
type KeyFormat ¶ added in v0.3.3
type KeyFormat struct { ErrorKey string MessageKey string ResultKey string ResultSizeKey string StatusKey string }
func CurrentFormat ¶ added in v0.3.3
func CurrentFormat() KeyFormat
CurrentFormat returns current key
type Response ¶
type Response interface { // set raw body Body(body interface{}) Response // Set Content type ContentType(contenttype string) Response // set data to response data Data(key string, value interface{}) Response // delete data value identified by key DeleteData(key string) Response // delete header by name DeleteHeader(name string) Response // sets `error` to response data Error(err interface{}) Response // returns response as []byte GetBytes() []byte HasData(key string) bool // set header with value Header(name, value string) Response // set html and content type (for template rendering) HTML(html string) Response // set http message Message(message string) Response // custom json marshalling function MarshalJSON() (result []byte, err error) // set `result` on response data (shorthand for Data("result", data)) Result(result interface{}) Response // set result_size on response ResultSize(size int) Response // set slice result (sets `result` and `result_size`) SliceResult(result interface{}) Response // set http status Status(status int) Response // return string value of response String() (body string) // Write complete response to writer Write(w http.ResponseWriter, request *http.Request) }
Response interface provides several method for Response
func Result ¶
func Result(result interface{}) Response
Result is helper to create status ok response.
func SliceResult ¶
func SliceResult(result interface{}) Response
SliceResult is helper to create status ok response.
func Unauthorized ¶
func Unauthorized() Response
Unauthorized returns response with StatusUnauthorized