Documentation
¶
Overview ¶
Package inrequest provides utilities for transforming HTTP request bodies into Go maps and structs.
It supports multiple content types including multipart/form-data, application/x-www-form-urlencoded, application/json, and query string parameters.
Basic Usage ¶
Parse form data from an HTTP request:
req := inrequest.FormData(r) defer req.Cleanup() // Optional: clean up temp files // Get as map data := req.ToMap() // Bind to struct var user User req.ToBind(&user) // Get as JSON jsonStr, _ := req.ToJsonString()
Request Types ¶
The package provides four main entry functions:
- FormData: Parse multipart/form-data or application/x-www-form-urlencoded
- FormDataWithOptions: Parse form data with custom options (e.g., max memory)
- Query: Parse query string parameters
- Json: Parse JSON request body
- Parse: Auto-detect content type and parse accordingly
Nested Data ¶
The package supports bracket notation for nested structures:
// Input: items[0][name]=foo&items[0][price]=10&items[1][name]=bar // Output: map[items:[]map[name:foo price:10] map[name:bar]]
File Uploads ¶
For file uploads, use FormData with multipart/form-data:
type Upload struct {
Title string `json:"title"`
Document *multipart.FileHeader `json:"document"` // Single file
Photos inrequest.FileHeaders `json:"photos"` // Multiple files
}
req := inrequest.FormData(r)
defer req.Cleanup()
var upload Upload
req.ToBind(&upload)
Type Conversion ¶
String values are automatically converted to appropriate Go types:
- "123" → int
- "3.14" → float64
- "true", "false" → bool
- "null" → nil
- Leading zeros preserved: "007" → "007" (string)
Error Handling ¶
The package provides custom error types for better error handling:
req := inrequest.FormData(r)
var user User
if err := req.ToBind(&user); err != nil {
if inrequest.IsBindError(err) {
// Handle binding error
}
}
Performance ¶
The library is optimized for performance with minimal allocations:
- Simple form (5 fields): ~5 µs
- Complex nested form: ~50 µs
- Large form (100 fields): ~200 µs
For high-traffic servers, call FormRequest.Cleanup to immediately release temporary files created during multipart parsing.
Index ¶
Constants ¶
const DefaultMaxMemory = 32 << 20
Default max memory for multipart form parsing (32MB)
Variables ¶
This section is empty.
Functions ¶
func IsBindError ¶ added in v1.3.0
IsBindError returns true if the error is a BindError
func IsParseError ¶ added in v1.3.0
IsParseError returns true if the error is a ParseError
Types ¶
type BindError ¶ added in v1.3.0
type BindError struct {
Field string // Field name that caused the error (if known)
Message string // Error message
Err error // Underlying error
}
BindError represents an error that occurred during struct binding
func NewBindError ¶ added in v1.3.0
NewBindError creates a new BindError
type FileHeaders ¶ added in v1.3.0
type FileHeaders []*multipart.FileHeader
FileHeaders represents multiple uploaded files for a single field
type FormRequest ¶ added in v1.2.2
type FormRequest struct {
// contains filtered or unexported fields
}
func FormData ¶ added in v1.1.0
func FormData(r *http.Request) FormRequest
FormData parses multipart/form-data and application/x-www-form-urlencoded requests. It automatically handles nested fields, arrays, and file uploads.
Basic usage:
form := inrequest.FormData(r) data := form.ToMap()
With struct binding:
var user User inrequest.FormData(r).ToBind(&user)
To clean up temporary files immediately (optional, for high-traffic servers):
form := inrequest.FormData(r) defer form.Cleanup()
func FormDataWithOptions ¶ added in v1.3.0
func FormDataWithOptions(r *http.Request, maxMemory int64) FormRequest
FormDataWithOptions parses form data with a custom max memory limit. maxMemory is the maximum bytes stored in memory (excess goes to temp files).
func (FormRequest) Cleanup ¶ added in v1.3.0
func (r FormRequest) Cleanup()
Cleanup removes temporary files created during multipart form parsing. This is optional - Go's garbage collector will eventually clean up, but calling this explicitly is recommended for high-traffic servers.
func (FormRequest) ToBind ¶ added in v1.2.2
func (r FormRequest) ToBind(model interface{}) error
ToBind binds the form data to a struct. It handles both regular fields via JSON marshaling and special types like *multipart.FileHeader.
func (FormRequest) ToJsonByte ¶ added in v1.2.2
func (r FormRequest) ToJsonByte() ([]byte, error)
func (FormRequest) ToJsonString ¶ added in v1.2.2
func (r FormRequest) ToJsonString() (string, error)
func (FormRequest) ToMap ¶ added in v1.2.2
func (r FormRequest) ToMap() RequestValue
type GroupRequest ¶
type GroupRequest map[string][]GroupRequestProperty
type GroupRequestProperty ¶
type GroupRequestProperty struct {
Path string
Value interface{}
}
type JsonRequest ¶ added in v1.2.2
type JsonRequest struct {
// contains filtered or unexported fields
}
func Json ¶ added in v1.1.0
func Json(r *http.Request) (JsonRequest, error)
Json parses a JSON request body.
Usage:
jsonReq, err := inrequest.Json(r)
if err != nil {
// handle error
}
data := jsonReq.ToMap()
func (JsonRequest) ToBind ¶ added in v1.2.2
func (r JsonRequest) ToBind(model interface{}) error
func (JsonRequest) ToByte ¶ added in v1.2.2
func (r JsonRequest) ToByte() ([]byte, error)
ToByte is deprecated, use ToJsonByte instead. Deprecated: Use ToJsonByte for consistent API.
func (JsonRequest) ToJsonByte ¶ added in v1.3.0
func (r JsonRequest) ToJsonByte() ([]byte, error)
func (JsonRequest) ToJsonString ¶ added in v1.3.0
func (r JsonRequest) ToJsonString() (string, error)
func (JsonRequest) ToMap ¶ added in v1.2.2
func (r JsonRequest) ToMap() RequestValue
func (JsonRequest) ToString ¶ added in v1.2.2
func (r JsonRequest) ToString() (string, error)
ToString is deprecated, use ToJsonString instead. Deprecated: Use ToJsonString for consistent API.
type ParseError ¶ added in v1.3.0
type ParseError struct {
Type string // Type of request being parsed (form, json, query)
Message string // Error message
Err error // Underlying error
}
ParseError represents an error that occurred during request parsing
func NewParseError ¶ added in v1.3.0
func NewParseError(reqType, message string, err error) *ParseError
NewParseError creates a new ParseError
func (*ParseError) Error ¶ added in v1.3.0
func (e *ParseError) Error() string
func (*ParseError) Unwrap ¶ added in v1.3.0
func (e *ParseError) Unwrap() error
type QueryRequest ¶ added in v1.2.2
type QueryRequest struct {
// contains filtered or unexported fields
}
func Query ¶ added in v1.1.0
func Query(r *http.Request) QueryRequest
Query parses URL query parameters.
Usage:
query := inrequest.Query(r) data := query.ToMap()
func (QueryRequest) ToBind ¶ added in v1.2.2
func (r QueryRequest) ToBind(model interface{}) error
func (QueryRequest) ToJsonByte ¶ added in v1.2.2
func (r QueryRequest) ToJsonByte() ([]byte, error)
func (QueryRequest) ToJsonString ¶ added in v1.2.2
func (r QueryRequest) ToJsonString() (string, error)
func (QueryRequest) ToMap ¶ added in v1.2.2
func (r QueryRequest) ToMap() RequestValue
type Request ¶ added in v1.3.0
type Request interface {
ToMap() RequestValue
ToBind(model interface{}) error
ToJsonByte() ([]byte, error)
ToJsonString() (string, error)
}
Request is the common interface for all request types
type RequestValue ¶
type RequestValue = map[string]interface{}
RequestValue is the map type used to store parsed request data