Bind decode req.Body or form-value to struct
(i interface{}, ctx dotweb.Context)
| 107 | |
| 108 | //Bind decode req.Body or form-value to struct |
| 109 | func (b *userBinder) Bind(i interface{}, ctx dotweb.Context) (err error) { |
| 110 | fmt.Println("UserBind.Bind") |
| 111 | req := ctx.Request() |
| 112 | ctype := req.Header.Get(dotweb.HeaderContentType) |
| 113 | if req.Body == nil { |
| 114 | err = errors.New("request body can't be empty") |
| 115 | return err |
| 116 | } |
| 117 | err = errors.New("request unsupported MediaType -> " + ctype) |
| 118 | switch { |
| 119 | case strings.HasPrefix(ctype, dotweb.MIMEApplicationJSON): |
| 120 | err = json.Unmarshal(ctx.Request().PostBody(), i) |
| 121 | case strings.HasPrefix(ctype, dotweb.MIMEApplicationXML): |
| 122 | err = xml.Unmarshal(ctx.Request().PostBody(), i) |
| 123 | //case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm), |
| 124 | // strings.HasPrefix(ctype, MIMETextHTML): |
| 125 | // err = reflects.ConvertMapToStruct(defaultTagName, i, ctx.FormValues()) |
| 126 | default: |
| 127 | //check is use json tag, fixed for issue #91 |
| 128 | tagName := "form" |
| 129 | if ctx.HttpServer().ServerConfig().EnabledBindUseJsonTag { |
| 130 | tagName = "json" |
| 131 | } |
| 132 | //no check content type for fixed issue #6 |
| 133 | err = reflects.ConvertMapToStruct(tagName, i, ctx.Request().FormValues()) |
| 134 | } |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | //BindJsonBody default use json decode req.Body to struct |
| 139 | func (b *userBinder) BindJsonBody(i interface{}, ctx dotweb.Context) (err error) { |
nothing calls this directly
no test coverage detected