github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/form.go (about) 1 // Copyright 2014 Manu Martinez-Almeida. All rights reserved. 2 // Use of this source code is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 package binding 6 7 import ( 8 "github.com/hellobchain/newcryptosm/http" 9 ) 10 11 const defaultMemory = 32 << 20 12 13 type formBinding struct{} 14 type formPostBinding struct{} 15 type formMultipartBinding struct{} 16 17 func (formBinding) Name() string { 18 return "form" 19 } 20 21 func (formBinding) Bind(req *http.Request, obj interface{}) error { 22 if err := req.ParseForm(); err != nil { 23 return err 24 } 25 if err := req.ParseMultipartForm(defaultMemory); err != nil { 26 if err != http.ErrNotMultipart { 27 return err 28 } 29 } 30 if err := mapForm(obj, req.Form); err != nil { 31 return err 32 } 33 return validate(obj) 34 } 35 36 func (formPostBinding) Name() string { 37 return "form-urlencoded" 38 } 39 40 func (formPostBinding) Bind(req *http.Request, obj interface{}) error { 41 if err := req.ParseForm(); err != nil { 42 return err 43 } 44 if err := mapForm(obj, req.PostForm); err != nil { 45 return err 46 } 47 return validate(obj) 48 } 49 50 func (formMultipartBinding) Name() string { 51 return "multipart/form-data" 52 } 53 54 func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error { 55 if err := req.ParseMultipartForm(defaultMemory); err != nil { 56 return err 57 } 58 if err := mappingByPtr(obj, (*multipartRequest)(req), "form"); err != nil { 59 return err 60 } 61 62 return validate(obj) 63 }