1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-02-15 13:53:35 +02:00

Merge pull request #395 from yuanfeng0905/fix-bind

Fix: bind
This commit is contained in:
Tony 2019-10-25 17:09:54 +08:00 committed by GitHub
commit 7713bccded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -274,9 +274,17 @@ func (c *Context) BindWith(obj interface{}, b binding.Binding) error {
return c.mustBindWith(obj, b)
}
// Bind bind req arg with defult form binding.
// Bind checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
// "application/json" --> JSON binding
// "application/xml" --> XML binding
// otherwise --> returns an error.
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
// It decodes the json payload into the struct specified as a pointer.
// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
func (c *Context) Bind(obj interface{}) error {
return c.mustBindWith(obj, binding.Form)
b := binding.Default(c.Request.Method, c.Request.Header.Get("Content-Type"))
return c.mustBindWith(obj, b)
}
// mustBindWith binds the passed struct pointer using the specified binding engine.