Skip to content

Commit

Permalink
Added support multipart/form-data gin-gonic#109
Browse files Browse the repository at this point in the history
  • Loading branch information
aadidenko authored and javierprovecho committed Mar 8, 2015
1 parent 0f46ae2 commit 0fb7bed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
24 changes: 20 additions & 4 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ type (
// XML binding
xmlBinding struct{}

// // form binding
// form binding
formBinding struct{}

// multipart form binding
multipartFormBinding struct{}
)

const MAX_MEMORY = 1 * 1024 * 1024

var (
JSON = jsonBinding{}
XML = xmlBinding{}
Form = formBinding{} // todo
JSON = jsonBinding{}
XML = xmlBinding{}
Form = formBinding{} // todo
MultipartForm = multipartFormBinding{}
)

func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error {
Expand Down Expand Up @@ -63,6 +69,16 @@ func (_ formBinding) Bind(req *http.Request, obj interface{}) error {
return Validate(obj)
}

func (_ multipartFormBinding) Bind(req *http.Request, obj interface{}) error {
if err := req.ParseMultipartForm(MAX_MEMORY); err != nil {
return err
}
if err := mapForm(obj, req.Form); err != nil {
return err
}
return Validate(obj)
}

func mapForm(ptr interface{}, form map[string][]string) error {
typ := reflect.TypeOf(ptr).Elem()
formStruct := reflect.ValueOf(ptr).Elem()
Expand Down
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ func (c *Context) Bind(obj interface{}) bool {
switch {
case c.Request.Method == "GET" || ctype == MIMEPOSTForm:
b = binding.Form
case ctype == MIMEMultipartPOSTForm:
b = binding.MultipartForm
case ctype == MIMEJSON:
b = binding.JSON
case ctype == MIMEXML || ctype == MIMEXML2:
Expand Down
15 changes: 8 additions & 7 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
)

const (
AbortIndex = math.MaxInt8 / 2
MIMEJSON = "application/json"
MIMEHTML = "text/html"
MIMEXML = "application/xml"
MIMEXML2 = "text/xml"
MIMEPlain = "text/plain"
MIMEPOSTForm = "application/x-www-form-urlencoded"
AbortIndex = math.MaxInt8 / 2
MIMEJSON = "application/json"
MIMEHTML = "text/html"
MIMEXML = "application/xml"
MIMEXML2 = "text/xml"
MIMEPlain = "text/plain"
MIMEPOSTForm = "application/x-www-form-urlencoded"
MIMEMultipartPOSTForm = "multipart/form-data"
)

type (
Expand Down

0 comments on commit 0fb7bed

Please sign in to comment.