Documentation
¶
Overview ¶
Package jsonhttp contains utility functions that make it easier to create JSON-based HTTP APIs.
Package jsonhttp provides convenience methods to provide better JSON HTTP APIs.
Index ¶
- Variables
- func Accepted(w http.ResponseWriter, response any)
- func BadGateway(w http.ResponseWriter, response any)
- func BadRequest(w http.ResponseWriter, response any)
- func Conflict(w http.ResponseWriter, response any)
- func Continue(w http.ResponseWriter, response any)
- func Created(w http.ResponseWriter, response any)
- func ExpectationFailed(w http.ResponseWriter, response any)
- func Forbidden(w http.ResponseWriter, response any)
- func Found(w http.ResponseWriter, response any)
- func GatewayTimeout(w http.ResponseWriter, response any)
- func Gone(w http.ResponseWriter, response any)
- func HTTPVersionNotSupported(w http.ResponseWriter, response any)
- func HandleBodyReadError(err error, w http.ResponseWriter) (responded bool)
- func HandleMethods(methods map[string]http.Handler, body string, contentType string, ...)
- func InternalServerError(w http.ResponseWriter, response any)
- func LengthRequired(w http.ResponseWriter, response any)
- func MethodNotAllowed(w http.ResponseWriter, response any)
- func MovedPermanently(w http.ResponseWriter, response any)
- func MultipleChoices(w http.ResponseWriter, response any)
- func NewMaxBodyBytesHandler(limit int64) func(http.Handler) http.Handler
- func NoContent(w http.ResponseWriter)
- func NonAuthoritativeInfo(w http.ResponseWriter, response any)
- func NotAcceptable(w http.ResponseWriter, response any)
- func NotFound(w http.ResponseWriter, response any)
- func NotFoundHandler(w http.ResponseWriter, _ *http.Request)
- func NotImplemented(w http.ResponseWriter, response any)
- func NotModified(w http.ResponseWriter, response any)
- func OK(w http.ResponseWriter, response any)
- func PartialContent(w http.ResponseWriter, response any)
- func PaymentRequired(w http.ResponseWriter, response any)
- func PermanentRedirect(w http.ResponseWriter, response any)
- func PreconditionFailed(w http.ResponseWriter, response any)
- func PreconditionRequired(w http.ResponseWriter, response any)
- func ProxyAuthRequired(w http.ResponseWriter, response any)
- func RequestEntityTooLarge(w http.ResponseWriter, response any)
- func RequestHeaderFieldsTooLarge(w http.ResponseWriter, response any)
- func RequestTimeout(w http.ResponseWriter, response any)
- func RequestURITooLong(w http.ResponseWriter, response any)
- func RequestedRangeNotSatisfiable(w http.ResponseWriter, response any)
- func ResetContent(w http.ResponseWriter, response any)
- func Respond(w http.ResponseWriter, statusCode int, response any)
- func SeeOther(w http.ResponseWriter, response any)
- func ServiceUnavailable(w http.ResponseWriter, response any)
- func SwitchingProtocols(w http.ResponseWriter, response any)
- func Teapot(w http.ResponseWriter, response any)
- func TemporaryRedirect(w http.ResponseWriter, response any)
- func TooManyRequests(w http.ResponseWriter, response any)
- func Unauthorized(w http.ResponseWriter, response any)
- func UnavailableForLegalReasons(w http.ResponseWriter, response any)
- func UnprocessableEntity(w http.ResponseWriter, response any)
- func UnsupportedMediaType(w http.ResponseWriter, response any)
- func UpgradeRequired(w http.ResponseWriter, response any)
- func UseProxy(w http.ResponseWriter, response any)
- type MethodHandler
- type Reason
- type StatusResponse
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultContentTypeHeader is the value of if "Content-Type" header // in HTTP response. DefaultContentTypeHeader = "application/json; charset=utf-8" // EscapeHTML specifies whether problematic HTML characters // should be escaped inside JSON quoted strings. EscapeHTML = false )
Functions ¶
func Accepted ¶
func Accepted(w http.ResponseWriter, response any)
Accepted writes a response with status code 202.
func BadGateway ¶
func BadGateway(w http.ResponseWriter, response any)
BadGateway writes a response with status code 502.
func BadRequest ¶
func BadRequest(w http.ResponseWriter, response any)
BadRequest writes a response with status code 400.
func Conflict ¶
func Conflict(w http.ResponseWriter, response any)
Conflict writes a response with status code 409.
func Continue ¶
func Continue(w http.ResponseWriter, response any)
Continue writes a response with status code 100.
func Created ¶
func Created(w http.ResponseWriter, response any)
Created writes a response with status code 201.
func ExpectationFailed ¶
func ExpectationFailed(w http.ResponseWriter, response any)
ExpectationFailed writes a response with status code 417.
func Forbidden ¶
func Forbidden(w http.ResponseWriter, response any)
Forbidden writes a response with status code 403.
func Found ¶
func Found(w http.ResponseWriter, response any)
Found writes a response with status code 302.
func GatewayTimeout ¶
func GatewayTimeout(w http.ResponseWriter, response any)
GatewayTimeout writes a response with status code 504.
func Gone ¶
func Gone(w http.ResponseWriter, response any)
Gone writes a response with status code 410.
func HTTPVersionNotSupported ¶
func HTTPVersionNotSupported(w http.ResponseWriter, response any)
HTTPVersionNotSupported writes a response with status code 505.
func HandleBodyReadError ¶
func HandleBodyReadError(err error, w http.ResponseWriter) (responded bool)
HandleBodyReadError checks for particular errors and writes appropriate response accordingly. If no known error is found, no response is written and the function returns false.
func HandleMethods ¶
func HandleMethods(methods map[string]http.Handler, body string, contentType string, w http.ResponseWriter, r *http.Request)
HandleMethods uses a corresponding Handler based on HTTP request method. If Handler is not found, a method not allowed HTTP response is returned with specified body and Content-Type header.
func InternalServerError ¶
func InternalServerError(w http.ResponseWriter, response any)
InternalServerError writes a response with status code 500.
func LengthRequired ¶
func LengthRequired(w http.ResponseWriter, response any)
LengthRequired writes a response with status code 411.
func MethodNotAllowed ¶
func MethodNotAllowed(w http.ResponseWriter, response any)
MethodNotAllowed writes a response with status code 405.
func MovedPermanently ¶
func MovedPermanently(w http.ResponseWriter, response any)
MovedPermanently writes a response with status code 301.
func MultipleChoices ¶
func MultipleChoices(w http.ResponseWriter, response any)
MultipleChoices writes a response with status code 300.
func NewMaxBodyBytesHandler ¶
NewMaxBodyBytesHandler is an http middleware constructor that limits the maximal number of bytes that can be read from the request body. When a body is read, the error can be handled with a helper function HandleBodyReadError in order to respond with Request Entity Too Large response. See TestNewMaxBodyBytesHandler as an example.
func NoContent ¶
func NoContent(w http.ResponseWriter)
NoContent writes a response with status code 204. It does not accept a response value since the HTTP server will not write it to the client when returning a NoContent response.
func NonAuthoritativeInfo ¶
func NonAuthoritativeInfo(w http.ResponseWriter, response any)
NonAuthoritativeInfo writes a response with status code 203.
func NotAcceptable ¶
func NotAcceptable(w http.ResponseWriter, response any)
NotAcceptable writes a response with status code 406.
func NotFound ¶
func NotFound(w http.ResponseWriter, response any)
NotFound writes a response with status code 404.
func NotFoundHandler ¶
func NotFoundHandler(w http.ResponseWriter, _ *http.Request)
func NotImplemented ¶
func NotImplemented(w http.ResponseWriter, response any)
NotImplemented writes a response with status code 501.
func NotModified ¶
func NotModified(w http.ResponseWriter, response any)
NotModified writes a response with status code 304.
func PartialContent ¶
func PartialContent(w http.ResponseWriter, response any)
PartialContent writes a response with status code 206.
func PaymentRequired ¶
func PaymentRequired(w http.ResponseWriter, response any)
PaymentRequired writes a response with status code 402.
func PermanentRedirect ¶
func PermanentRedirect(w http.ResponseWriter, response any)
PermanentRedirect writes a response with status code 308.
func PreconditionFailed ¶
func PreconditionFailed(w http.ResponseWriter, response any)
PreconditionFailed writes a response with status code 412.
func PreconditionRequired ¶
func PreconditionRequired(w http.ResponseWriter, response any)
PreconditionRequired writes a response with status code 428.
func ProxyAuthRequired ¶
func ProxyAuthRequired(w http.ResponseWriter, response any)
ProxyAuthRequired writes a response with status code 407.
func RequestEntityTooLarge ¶
func RequestEntityTooLarge(w http.ResponseWriter, response any)
RequestEntityTooLarge writes a response with status code 413.
func RequestHeaderFieldsTooLarge ¶
func RequestHeaderFieldsTooLarge(w http.ResponseWriter, response any)
RequestHeaderFieldsTooLarge writes a response with status code 431.
func RequestTimeout ¶
func RequestTimeout(w http.ResponseWriter, response any)
RequestTimeout writes a response with status code 408.
func RequestURITooLong ¶
func RequestURITooLong(w http.ResponseWriter, response any)
RequestURITooLong writes a response with status code 414.
func RequestedRangeNotSatisfiable ¶
func RequestedRangeNotSatisfiable(w http.ResponseWriter, response any)
RequestedRangeNotSatisfiable writes a response with status code 416.
func ResetContent ¶
func ResetContent(w http.ResponseWriter, response any)
ResetContent writes a response with status code 205.
func Respond ¶
func Respond(w http.ResponseWriter, statusCode int, response any)
Respond writes a JSON-encoded body to http.ResponseWriter.
func SeeOther ¶
func SeeOther(w http.ResponseWriter, response any)
SeeOther writes a response with status code 303.
func ServiceUnavailable ¶
func ServiceUnavailable(w http.ResponseWriter, response any)
ServiceUnavailable writes a response with status code 503.
func SwitchingProtocols ¶
func SwitchingProtocols(w http.ResponseWriter, response any)
SwitchingProtocols writes a response with status code 101.
func Teapot ¶
func Teapot(w http.ResponseWriter, response any)
Teapot writes a response with status code 418.
func TemporaryRedirect ¶
func TemporaryRedirect(w http.ResponseWriter, response any)
TemporaryRedirect writes a response with status code 307.
func TooManyRequests ¶
func TooManyRequests(w http.ResponseWriter, response any)
TooManyRequests writes a response with status code 429.
func Unauthorized ¶
func Unauthorized(w http.ResponseWriter, response any)
Unauthorized writes a response with status code 401.
func UnavailableForLegalReasons ¶
func UnavailableForLegalReasons(w http.ResponseWriter, response any)
UnavailableForLegalReasons writes a response with status code 451.
func UnprocessableEntity ¶
func UnprocessableEntity(w http.ResponseWriter, response any)
UnprocessableEntity writes a response with status code 422.
func UnsupportedMediaType ¶
func UnsupportedMediaType(w http.ResponseWriter, response any)
UnsupportedMediaType writes a response with status code 415.
func UpgradeRequired ¶
func UpgradeRequired(w http.ResponseWriter, response any)
UpgradeRequired writes a response with status code 426.
func UseProxy ¶
func UseProxy(w http.ResponseWriter, response any)
UseProxy writes a response with status code 305.
Types ¶
type MethodHandler ¶
func (MethodHandler) ServeHTTP ¶
func (h MethodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type StatusResponse ¶
type StatusResponse struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Reasons []Reason `json:"reasons,omitempty"`
}
StatusResponse is a standardized error format for specific HTTP responses. Code field corresponds with HTTP status code, and Message field is a short description of that code or provides more context about the reason for such response.
If response is string, error or Stringer type the string will be set as value to the Message field.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package jsonhttptest helps with end-to-end testing of JSON-based HTTP APIs.
|
Package jsonhttptest helps with end-to-end testing of JSON-based HTTP APIs. |