form.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. package binding
  5. import "net/http"
  6. type formBinding struct{}
  7. type formPostBinding struct{}
  8. type formMultipartBinding struct{}
  9. func (formBinding) Name() string {
  10. return "form"
  11. }
  12. func (formBinding) Bind(req *http.Request, obj interface{}) error {
  13. if err := req.ParseForm(); err != nil {
  14. return err
  15. }
  16. req.ParseMultipartForm(32 << 10) // 32 MB
  17. if err := mapForm(obj, req.Form); err != nil {
  18. return err
  19. }
  20. return validate(obj)
  21. }
  22. func (formPostBinding) Name() string {
  23. return "form-urlencoded"
  24. }
  25. func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
  26. if err := req.ParseForm(); err != nil {
  27. return err
  28. }
  29. if err := mapForm(obj, req.PostForm); err != nil {
  30. return err
  31. }
  32. return validate(obj)
  33. }
  34. func (formMultipartBinding) Name() string {
  35. return "multipart/form-data"
  36. }
  37. func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
  38. if err := req.ParseMultipartForm(32 << 10); err != nil {
  39. return err
  40. }
  41. if err := mapForm(obj, req.MultipartForm.Value); err != nil {
  42. return err
  43. }
  44. return validate(obj)
  45. }