parser.go 558 B

123456789101112131415161718192021222324252627
  1. package encoding
  2. import (
  3. "net/http"
  4. "net/textproto"
  5. "github.com/zeromicro/go-zero/core/mapping"
  6. )
  7. const headerKey = "header"
  8. var headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValues(),
  9. mapping.WithCanonicalKeyFunc(textproto.CanonicalMIMEHeaderKey))
  10. // ParseHeaders parses the headers request.
  11. func ParseHeaders(header http.Header, v interface{}) error {
  12. m := map[string]interface{}{}
  13. for k, v := range header {
  14. if len(v) == 1 {
  15. m[k] = v[0]
  16. } else {
  17. m[k] = v
  18. }
  19. }
  20. return headerUnmarshaler.Unmarshal(m, v)
  21. }