Documentation
¶
Index ¶
Constants ¶
View Source
const ( ErrMaxBodySizeExceeded = "json body size exceeds the maximum allowed size, limit is %d bytes" ErrSyntaxError = "json body contains badly-formed JSON at position %d" ErrUnknownField = "json body contains an unknown field %s" )
View Source
const (
ErrInvalidContentTypeField = "Content-Type"
)
Variables ¶
View Source
var ( ErrCodeInvalidContentType string ErrCodeNilDestination string ErrCodeFailedToReadBody string ErrCodeUnmarshalRequestBodyFailed string ErrCodeSyntaxError string ErrCodeUnmarshalTypeError string ErrCodeUnknownField string ErrCodeEmptyBody string ErrCodeMaxBodySizeExceeded string )
View Source
var ( ErrUnexpectedEOF = errors.New("json body contains badly-formed JSON") ErrEmptyBody = errors.New("json body is empty") ErrUnmarshalBodyFailed = errors.New("failed to unmarshal json body") )
View Source
var ( ErrNilDestination = errors.New("json destination is nil") ErrInvalidContentType = errors.New("invalid content type, expected application/json") )
Functions ¶
func BodyDecodeErrorHandler ¶
func BodyDecodeErrorHandler( w http.ResponseWriter, err error, ) error
BodyDecodeErrorHandler handles the error on JSON body decoding
Parameters:
- w: The HTTP response writer
- err: The error that occurred during decoding
Returns:
- error: An error if the encoder is nil or if encoding the response fails
func CheckContentType ¶
CheckContentType checks if the content type is JSON
Parameters:
- r: The HTTP request
Returns:
- bool: True if the content type is JSON, false otherwise
Types ¶
type Decoder ¶ added in v0.11.2
type Decoder struct {
// contains filtered or unexported fields
}
Decoder struct
func NewDecoder ¶ added in v0.11.2
func NewDecoder( mode *goflagsmode.Flag, ) *Decoder
NewDecoder creates a new JSON decoder
Parameters:
- mode: The flag mode
Returns:
- *Decoder: The default decoder
type StreamDecoder ¶ added in v0.11.2
type StreamDecoder struct {
// contains filtered or unexported fields
}
StreamDecoder is the JSON decoder struct
func NewStreamDecoder ¶ added in v0.11.2
func NewStreamDecoder( mode *goflagsmode.Flag, ) *StreamDecoder
NewStreamDecoder creates a new JSON decoder
Parameters:
- mode: The flag mode
- responsesHandler: The HTTP handler to handle errors
Returns:
- *StreamDecoder: The default decoder
func (StreamDecoder) Decode ¶ added in v0.11.2
func (s StreamDecoder) Decode( w http.ResponseWriter, r *http.Request, dest interface{}, ) error
Decode decodes the JSON request body and stores it in the destination
Parameters:
- w: The HTTP response writer
- r: The HTTP request
- dest: The destination to store the decoded body
Returns:
- error: The error if any
Click to show internal directories.
Click to hide internal directories.