jsonhttp

package
v1.13.1-rc1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2023 License: BSD-3-Clause Imports: 6 Imported by: 5

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

Constants

This section is empty.

Variables

View Source
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 interface{})

Accepted writes a response with status code 202.

func BadGateway

func BadGateway(w http.ResponseWriter, response interface{})

BadGateway writes a response with status code 502.

func BadRequest

func BadRequest(w http.ResponseWriter, response interface{})

BadRequest writes a response with status code 400.

func Conflict

func Conflict(w http.ResponseWriter, response interface{})

Conflict writes a response with status code 409.

func Continue

func Continue(w http.ResponseWriter, response interface{})

Continue writes a response with status code 100.

func Created

func Created(w http.ResponseWriter, response interface{})

Created writes a response with status code 201.

func ExpectationFailed

func ExpectationFailed(w http.ResponseWriter, response interface{})

ExpectationFailed writes a response with status code 417.

func Forbidden

func Forbidden(w http.ResponseWriter, response interface{})

Forbidden writes a response with status code 403.

func Found

func Found(w http.ResponseWriter, response interface{})

Found writes a response with status code 302.

func GatewayTimeout

func GatewayTimeout(w http.ResponseWriter, response interface{})

GatewayTimeout writes a response with status code 504.

func Gone

func Gone(w http.ResponseWriter, response interface{})

Gone writes a response with status code 410.

func HTTPVersionNotSupported

func HTTPVersionNotSupported(w http.ResponseWriter, response interface{})

HTTPVersionNotSupported writes a response with status code 505.

func HandleBodyReadError added in v0.2.0

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 added in v1.7.0

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 interface{})

InternalServerError writes a response with status code 500.

func LengthRequired

func LengthRequired(w http.ResponseWriter, response interface{})

LengthRequired writes a response with status code 411.

func MethodNotAllowed

func MethodNotAllowed(w http.ResponseWriter, response interface{})

MethodNotAllowed writes a response with status code 405.

func MovedPermanently

func MovedPermanently(w http.ResponseWriter, response interface{})

MovedPermanently writes a response with status code 301.

func MultipleChoices

func MultipleChoices(w http.ResponseWriter, response interface{})

MultipleChoices writes a response with status code 300.

func NewMaxBodyBytesHandler added in v0.2.0

func NewMaxBodyBytesHandler(limit int64) func(http.Handler) http.Handler

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 added in v0.2.0

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 interface{})

NonAuthoritativeInfo writes a response with status code 203.

func NotAcceptable

func NotAcceptable(w http.ResponseWriter, response interface{})

NotAcceptable writes a response with status code 406.

func NotFound

func NotFound(w http.ResponseWriter, response interface{})

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 interface{})

NotImplemented writes a response with status code 501.

func NotModified

func NotModified(w http.ResponseWriter, response interface{})

NotModified writes a response with status code 304.

func OK

func OK(w http.ResponseWriter, response interface{})

OK writes a response with status code 200.

func PartialContent

func PartialContent(w http.ResponseWriter, response interface{})

PartialContent writes a response with status code 206.

func PaymentRequired

func PaymentRequired(w http.ResponseWriter, response interface{})

PaymentRequired writes a response with status code 402.

func PermanentRedirect

func PermanentRedirect(w http.ResponseWriter, response interface{})

PermanentRedirect writes a response with status code 308.

func PreconditionFailed

func PreconditionFailed(w http.ResponseWriter, response interface{})

PreconditionFailed writes a response with status code 412.

func PreconditionRequired

func PreconditionRequired(w http.ResponseWriter, response interface{})

PreconditionRequired writes a response with status code 428.

func ProxyAuthRequired

func ProxyAuthRequired(w http.ResponseWriter, response interface{})

ProxyAuthRequired writes a response with status code 407.

func RequestEntityTooLarge

func RequestEntityTooLarge(w http.ResponseWriter, response interface{})

RequestEntityTooLarge writes a response with status code 413.

func RequestHeaderFieldsTooLarge

func RequestHeaderFieldsTooLarge(w http.ResponseWriter, response interface{})

RequestHeaderFieldsTooLarge writes a response with status code 431.

func RequestTimeout

func RequestTimeout(w http.ResponseWriter, response interface{})

RequestTimeout writes a response with status code 408.

func RequestURITooLong

func RequestURITooLong(w http.ResponseWriter, response interface{})

RequestURITooLong writes a response with status code 414.

func RequestedRangeNotSatisfiable

func RequestedRangeNotSatisfiable(w http.ResponseWriter, response interface{})

RequestedRangeNotSatisfiable writes a response with status code 416.

func ResetContent

func ResetContent(w http.ResponseWriter, response interface{})

ResetContent writes a response with status code 205.

func Respond

func Respond(w http.ResponseWriter, statusCode int, response interface{})

Respond writes a JSON-encoded body to http.ResponseWriter.

func SeeOther

func SeeOther(w http.ResponseWriter, response interface{})

SeeOther writes a response with status code 303.

func ServiceUnavailable

func ServiceUnavailable(w http.ResponseWriter, response interface{})

ServiceUnavailable writes a response with status code 503.

func SwitchingProtocols

func SwitchingProtocols(w http.ResponseWriter, response interface{})

SwitchingProtocols writes a response with status code 101.

func Teapot

func Teapot(w http.ResponseWriter, response interface{})

Teapot writes a response with status code 418.

func TemporaryRedirect

func TemporaryRedirect(w http.ResponseWriter, response interface{})

TemporaryRedirect writes a response with status code 307.

func TooManyRequests

func TooManyRequests(w http.ResponseWriter, response interface{})

TooManyRequests writes a response with status code 429.

func Unauthorized

func Unauthorized(w http.ResponseWriter, response interface{})

Unauthorized writes a response with status code 401.

func UnavailableForLegalReasons

func UnavailableForLegalReasons(w http.ResponseWriter, response interface{})

UnavailableForLegalReasons writes a response with status code 451.

func UnprocessableEntity added in v1.10.0

func UnprocessableEntity(w http.ResponseWriter, response interface{})

UnprocessableEntity writes a response with status code 422.

func UnsupportedMediaType

func UnsupportedMediaType(w http.ResponseWriter, response interface{})

UnsupportedMediaType writes a response with status code 415.

func UpgradeRequired

func UpgradeRequired(w http.ResponseWriter, response interface{})

UpgradeRequired writes a response with status code 426.

func UseProxy

func UseProxy(w http.ResponseWriter, response interface{})

UseProxy writes a response with status code 305.

Types

type MethodHandler

type MethodHandler map[string]http.Handler

func (MethodHandler) ServeHTTP

func (h MethodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Reason added in v1.10.0

type Reason struct {
	Field string `json:"field"`
	Error string `json:"error"`
}

Reason represents a reason for an invalid request entry.

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.

Jump to

Keyboard shortcuts

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