Skip to content

Commit bf65e92

Browse files
committed
fix: valdate after parse body
1 parent 6ef327a commit bf65e92

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

context.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,11 @@ func (c *Context) ClientIP() string {
190190
// It decodes the json payload into the struct specified as a pointer.
191191
// Like ParseBody() but this method also writes a 400 error if the json is not valid.
192192
func (c *Context) Bind(data BindingStruct) error {
193-
err := data.Validate()
194-
if err != nil {
193+
binding := bindDefault(c.Request.Method, c.ContentType())
194+
if err := binding.Bind(c.Request, data); err != nil {
195195
return err
196196
}
197-
198-
binding := bindDefault(c.Request.Method, c.ContentType())
199-
return binding.Bind(c.Request, data)
197+
return data.Validate()
200198
}
201199

202200
// WriteHeader sends an HTTP response header with status code.
@@ -307,6 +305,20 @@ func (c *Context) SetResult(code int, msg string) {
307305
}
308306
}
309307

308+
// SetBody return json body
309+
func (c *Context) SetBody(data interface{}) {
310+
rsp := map[string]interface{}{
311+
"code": 0,
312+
"msg": "ok",
313+
"data": data,
314+
}
315+
316+
if err := renderJSON(c.ResponseWriter, rsp); err != nil {
317+
panic(err)
318+
}
319+
}
320+
321+
// HTML rendder html resp
310322
func (c *Context) HTML(name string, data interface{}) {
311323
if err := renderHTML(c.ResponseWriter, c.template, name, data); err != nil {
312324
panic(err)

0 commit comments

Comments
 (0)