Documentation ¶
Overview ¶
Package request implements reflection-based net/http request decoder.
Index ¶
- Variables
- func DecoderMiddleware(factory DecoderMaker) func(http.Handler) http.Handler
- func ValidatorMiddleware(factory rest.RequestValidatorFactory) func(http.Handler) http.Handler
- type DecoderFactory
- func (df *DecoderFactory) MakeDecoder(method string, input interface{}, customMapping rest.RequestMapping) nethttp.RequestDecoder
- func (df *DecoderFactory) RegisterFunc(fn form.DecodeFunc, types ...interface{})
- func (df *DecoderFactory) SetDecoderFunc(tagName rest.ParamIn, d func(r *http.Request) (url.Values, error))
- type DecoderFunc
- type DecoderMaker
- type EmbeddedSetter
- type Loader
- type Setter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrJSONExpected = errors.New("request with application/json content type expected") ErrMissingRequestBody = errors.New("missing request body") ErrMissingRequiredFile = errors.New("missing required file") )
These errors may be returned on request decoding failure.
Functions ¶
func DecoderMiddleware ¶
func DecoderMiddleware(factory DecoderMaker) func(http.Handler) http.Handler
DecoderMiddleware sets up request decoder in suitable handlers.
Types ¶
type DecoderFactory ¶
type DecoderFactory struct { // ApplyDefaults enables default value assignment for fields missing explicit value in request. // Default value is retrieved from `default` field tag. ApplyDefaults bool // JSONReader allows custom JSON decoder for request body. // If not set encoding/json.Decoder is used. JSONReader func(rd io.Reader, v interface{}) error // JSONSchemaReflector is optional, it is called to infer "default" values. JSONSchemaReflector *jsonschema.Reflector // contains filtered or unexported fields }
DecoderFactory decodes http requests.
Please use NewDecoderFactory to create instance.
func NewDecoderFactory ¶
func NewDecoderFactory() *DecoderFactory
NewDecoderFactory creates request decoder factory.
func (*DecoderFactory) MakeDecoder ¶
func (df *DecoderFactory) MakeDecoder( method string, input interface{}, customMapping rest.RequestMapping, ) nethttp.RequestDecoder
MakeDecoder creates request.RequestDecoder for a http method and request structure.
Input is checked for `json`, `file` tags only for methods with body semantics (POST, PUT, PATCH) or if input implements openapi.RequestBodyEnforcer.
CustomMapping can be nil, otherwise it is used instead of field tags to match decoded fields with struct.
func (*DecoderFactory) RegisterFunc ¶
func (df *DecoderFactory) RegisterFunc(fn form.DecodeFunc, types ...interface{})
RegisterFunc adds custom type handling.
func (*DecoderFactory) SetDecoderFunc ¶
func (df *DecoderFactory) SetDecoderFunc(tagName rest.ParamIn, d func(r *http.Request) (url.Values, error))
SetDecoderFunc adds custom decoder function for values of particular field tag name.
type DecoderFunc ¶
DecoderFunc implements RequestDecoder with a func.
type DecoderMaker ¶
type DecoderMaker interface {
MakeDecoder(method string, input interface{}, customMapping rest.RequestMapping) nethttp.RequestDecoder
}
DecoderMaker creates request decoder for particular structured Go input value.
type EmbeddedSetter ¶
type EmbeddedSetter struct {
// contains filtered or unexported fields
}
EmbeddedSetter can capture *http.Resuest in your input structure.
func (*EmbeddedSetter) Request ¶
func (e *EmbeddedSetter) Request() *http.Request
Request is an accessor.
Example ¶
package main import ( "fmt" "net/http" "github.com/dikac/swaggest-rest/request" ) func main() { type MyRequest struct { request.EmbeddedSetter Foo int `header:"X-Foo"` Bar string `formData:"bar"` Baz bool `query:"baz"` } // A decoder for particular structure, can be reused for multiple HTTP requests. myDecoder := request.NewDecoderFactory().MakeDecoder(http.MethodPost, new(MyRequest), nil) // Request and response writer from ServeHTTP. var ( rw http.ResponseWriter req *http.Request ) // This code would presumably live in ServeHTTP. var myReq MyRequest if err := myDecoder.Decode(req, &myReq, nil); err != nil { http.Error(rw, err.Error(), http.StatusBadRequest) } // Access data from raw request. fmt.Println("Remote Addr:", myReq.Request().RemoteAddr) }
Output:
func (*EmbeddedSetter) SetRequest ¶
func (e *EmbeddedSetter) SetRequest(r *http.Request)
SetRequest implements Setter.