Documentation ¶
Index ¶
- Constants
- Variables
- func BindForm(r *http.Request, v interface{}) error
- func BindFormMultipart(r *http.Request, v interface{}) error
- func BindFunc(r *http.Request, v interface{}) error
- func BindJSON(r *http.Request, v interface{}) error
- func BindQuery(r *http.Request, v interface{}) error
- func FileDataConverter(v string) reflect.Value
- func FileDataConverterPtr(v string) reflect.Value
- func GetFileMimeType(input []byte) (string, error)
- type Binder
- type BinderFunc
- type DefaultBinder
- type File
- type FileData
Constants ¶
const ( // JSON struct tag name for binding TagJSON = "json" // Form struct tag name for binding TagForm = "form" // Query struct tag name for binding TagQuery = "query" // Header struct tag name for binding TagHeader = "header" )
Default tag names for binding
Variables ¶
var ( ErrInvalidMethod = errors.New("invalid http method for binding") ErrInvalidContentType = errors.New("invalid content type for binding") ErrEmptyBody = errors.New("empty request body") ErrParseForm = errors.New("failed to parse form") ErrDecodeForm = errors.New("failed to decode form") ErrGetFile = errors.New("failed to get file from request") ErrReadFile = errors.New("failed to read file from request") ErrGetFileMimeType = errors.New("failed to get file mime type") ErrEmptyQuery = errors.New("empty query string") ErrDecodeQuery = errors.New("failed to decode request query") ErrInvalidInput = errors.New("invalid input") ErrUnsupportedType = errors.New("unsupported type") ErrTargetMustBeAPointer = errors.New("target must be a pointer") ErrTargetMustBeAStruct = errors.New("target must be a struct") ErrInputIsNil = errors.New("input is nil") ErrDecodeJSON = errors.New("failed to decode json") )
Predefined errors
var MultiPartFormMaxMemory int64 = 32 << 20
MultiPartFormMaxMemory is the maximum amount of memory to use when parsing a multipart form. It is passed to http.Request.ParseMultipartForm. Default value is 32 << 20 (32 MB).
Functions ¶
func BindForm ¶
BindForm binds the passed v pointer to the request. It uses the application/x-www-form-urlencoded content type for binding. `v` param should be a pointer to a struct with `form“ tags. Implements the binder.BinderFunc interface.
func BindFormMultipart ¶
BindFormMultipart binds the passed v pointer to the request. It uses the multipart/form-data content type for binding. `v` param should be a pointer to a struct with `form“ tags. Implements the binder.BinderFunc interface.
func BindFunc ¶ added in v0.2.0
BindFunc is the function type that implements the BinderFunc interface. It returns an error if the binding fails. Binding depends on the request method and the content type. If the request method is GET, HEAD, DELETE, or OPTIONS, then the binding is done from the query. If the request method is POST, PUT, or PATCH, then the binding is done from the request body. If the content type is JSON, then the binding is done from the request body. If the content type is form, then the binding is done from the request body.
func BindJSON ¶
BindJSON binds the passed v pointer to the request. It uses the JSON content type for binding. `v` param should be a pointer to a struct with `json` tags. Implements the binder.BinderFunc interface.
func BindQuery ¶
BindQuery binds the passed v pointer to the request. It uses the query string for binding. `v` param should be a pointer to a struct with `query“ tags. Implements the binder.BinderFunc interface.
func FileDataConverter ¶
FileDataConverter is the function type that converts a string to a reflect.Value. The string is the file data.
func FileDataConverterPtr ¶
FileDataConverterPtr is the function type that converts a string to a reflect.Value. The string is the file data.
func GetFileMimeType ¶
GetFileMimeType returns the MIME type of a file using the github.com/gabriel-vasile/mimetype package.
Types ¶
type Binder ¶
Binder is the interface that wraps the Bind method.
Bind should bind the passed v pointer to the request. For example, the implementation could bind the request body to the v pointer.
type BinderFunc ¶
BinderFunc is the function type that implements the Binder interface.
type DefaultBinder ¶ added in v0.2.0
type DefaultBinder struct{}
DefaultBinder is the default implementation of the Binder interface.
func (*DefaultBinder) Bind ¶ added in v0.2.0
func (b *DefaultBinder) Bind(r *http.Request, v interface{}) error
Bind binds the passed v pointer to the request. For example, the implementation could bind the request body to the v pointer. Bind implements the Binder interface. It returns an error if the binding fails. Binding depends on the request method and the content type. If the request method is GET, HEAD, DELETE, or OPTIONS, then the binding is done from the query. If the request method is POST, PUT, or PATCH, then the binding is done from the request body. If the content type is JSON, then the binding is done from the request body. If the content type is form, then the binding is done from the request body.
type File ¶
type File struct { // FileName stores the name of the file FileName string // FileSize stores the size of the file in bytes FileSize int64 // ContentType stores the MIME type of the file ContentType string // Data is a byte slice that holds the contents of the file Data []byte }
File represents a file that was uploaded via a multipart form.
type FileData ¶
type FileData struct { // Name is the filename. Name string // Size is the size of the file in bytes. Size int64 // MimeType is the MIME type of the file, as determined by its contents or extension. MimeType string // Data contains the raw bytes of the file. Data []byte }
FileData represents the data contained in a file, including metadata and the file contents.
func GetFileData ¶
GetFileData extracts file data from a multipart request.
func GetFileDataFromMultipartFileHeader ¶
func GetFileDataFromMultipartFileHeader(header *multipart.FileHeader) (*FileData, error)
GetFileDataFromMultipartFileHeader extracts file data from a multipart file header.