Restored support of multipart/form-data
This commit is contained in:
@ -28,9 +28,10 @@ type Binding interface {
|
||||
var validate = validator.New("binding", validator.BakedInValidators)
|
||||
|
||||
var (
|
||||
JSON = jsonBinding{}
|
||||
XML = xmlBinding{}
|
||||
Form = formBinding{}
|
||||
XML = xmlBinding{}
|
||||
JSON = jsonBinding{}
|
||||
Form = formBinding{}
|
||||
MultipartForm = multipartFormBinding{}
|
||||
)
|
||||
|
||||
func Default(method, contentType string) Binding {
|
||||
@ -42,6 +43,8 @@ func Default(method, contentType string) Binding {
|
||||
return JSON
|
||||
case MIMEXML, MIMEXML2:
|
||||
return XML
|
||||
case MIMEMultipartPOSTForm:
|
||||
return MultipartForm
|
||||
default:
|
||||
return Form
|
||||
}
|
||||
|
@ -33,6 +33,9 @@ func TestBindingDefault(t *testing.T) {
|
||||
|
||||
assert.Equal(t, Default("POST", MIMEPOSTForm), Form)
|
||||
assert.Equal(t, Default("DELETE", MIMEPOSTForm), Form)
|
||||
|
||||
assert.Equal(t, Default("POST", MIMEMultipartPOSTForm), MultipartForm)
|
||||
assert.Equal(t, Default("DELETE", MIMEMultipartPOSTForm), MultipartForm)
|
||||
}
|
||||
|
||||
func TestBindingJSON(t *testing.T) {
|
||||
|
25
binding/form_multipart.go
Normal file
25
binding/form_multipart.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import "net/http"
|
||||
|
||||
const MAX_MEMORY = 1 * 1024 * 1024
|
||||
|
||||
type multipartFormBinding struct{}
|
||||
|
||||
func (_ multipartFormBinding) Name() string {
|
||||
return "multipart form"
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user