requestbody

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Go Request Body Middleware

RequestBodyHandler is middleware for handling content encoding and limiting allowed content length.

When configuring the middleware, you can specify options such as maximum content length, supported encodings, and an error handler. By default, gzip and deflate encodings are supported and the request content length is limited to 10MB.

Configuration can be overridden by handlers on a per-request basis.

import (
    "net/http"
    "github.com/danielrbradley/go-request-body"
)

func main() {
    r := http.NewServeMux()

    r.HandleFunc("/", PostData)

    // Wrap our server to handle compressed requests with default limits.
    middleware := requestbody.RequestBodyHandler(r,
      requestbody.ContentLengthLimit(4*1024*1024), // 4MB
      requestbody.RequireContentLength(true))

    http.ListenAndServe(":8000", requestbody.RequestBodyHandler(r))
}

func PostData(w http.ResponseWriter, r *http.Request) {
  // Increase allowed request size for this request
  SetRequestBodyOption(r, requestbody.ContentLengthLimit(100*1024*1024))

  w.WriteHeader(http.StatusOK)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultOnError

func DefaultOnError(w http.ResponseWriter, r *http.Request, err error) error

DefaultOnError is the default error handler for the lazyReader. It handles specific errors like MaxBytesError and sets the appropriate HTTP status. For other errors, it simply returns the error.

func DeflateEncodingReader

func DeflateEncodingReader(r io.Reader) (io.ReadCloser, error)

func GZipEncodingReader

func GZipEncodingReader(r io.Reader) (io.ReadCloser, error)

func RequestBodyHandler

func RequestBodyHandler(h http.Handler, defaults ...Option) http.Handler

RequestBodyHandler is middleware for handling content encoding and content length. When configuring the handler, you can specify options such as maximum content length, supported encodings, and an error handler.

The configuration is only evaluated at the point of reading the request body, giving handlers the opportunity to override the defaults on a per-request basis.

Wrapped handlers can override the default options on a per-request basis using the `SetRequestBodyOption` function to set options on the request context.

func SetRequestBodyOption

func SetRequestBodyOption(r *http.Request, opts ...Option)

Types

type BadRequestError

type BadRequestError struct {
	Err error
}

func (*BadRequestError) Error

func (e *BadRequestError) Error() string

func (*BadRequestError) RecommendedStatusCode

func (e *BadRequestError) RecommendedStatusCode() int

type Encoding

type Encoding struct {
	// contains filtered or unexported fields
}

type EncodingReader

type EncodingReader func(r io.Reader) (io.ReadCloser, error)

type Option

type Option interface {
	// contains filtered or unexported methods
}

func ContentLengthLimit

func ContentLengthLimit(maxContentLength int64) Option

func DisableEncoding

func DisableEncoding(name string) Option

func OnError

func OnError(fn func(w http.ResponseWriter, r *http.Request, err error) error) Option

func RequireContentLength

func RequireContentLength(require bool) Option

func SupportEncoding

func SupportEncoding(name string, reader EncodingReader) Option

type RequestBodyError

type RequestBodyError interface {
	Error() string
	RecommendedStatusCode() int
}

type RequestContentLengthRequiredError

type RequestContentLengthRequiredError struct {
}

func (*RequestContentLengthRequiredError) Error

func (*RequestContentLengthRequiredError) RecommendedStatusCode

func (e *RequestContentLengthRequiredError) RecommendedStatusCode() int

type RequestContentTooLargeError

type RequestContentTooLargeError struct {
	Limit int64
}

func (*RequestContentTooLargeError) Error

func (*RequestContentTooLargeError) RecommendedStatusCode

func (e *RequestContentTooLargeError) RecommendedStatusCode() int

type RequestUnsupportedMediaTypeError

type RequestUnsupportedMediaTypeError struct {
	Encoding string
}

func (*RequestUnsupportedMediaTypeError) Error

func (*RequestUnsupportedMediaTypeError) RecommendedStatusCode

func (e *RequestUnsupportedMediaTypeError) RecommendedStatusCode() int

Jump to

Keyboard shortcuts

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