-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
35 lines (33 loc) · 1.02 KB
/
bind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package web
import (
"fmt"
"net/http"
"strings"
)
// Bind automatically deserializes the request body into the specified value.
//
// `value` must be a pointer to the value to be deserialized.
//
// The decoder is automatically selected based on the "Content-Type" header in
// the request.
//
// Currently supported content types are:
// - JSON
// - XML
// - YAML
func Bind(req *http.Request, value interface{}) (err error) {
full := req.Header.Get("Content-Type")
// Support impure content-type values such as "application/json; charset=utf-8".
contentType := strings.ToLower(strings.TrimSpace(strings.Split(full, ";")[0]))
switch {
case contentType == MimeJson:
err = DecodeJson(req.Body, value)
case contentType == MimeXml || contentType == MimeXml2:
err = DecodeXml(req.Body, value)
case contentType == MimeYaml || contentType == MimeYaml2 || contentType == MimeYaml3:
err = DecodeYaml(req.Body, value)
default:
err = fmt.Errorf("bind failed; unable to handle content-type=%s", contentType)
}
return
}