From beaeb85b017ad8266ebeaeaedf520c51fe4e7101 Mon Sep 17 00:00:00 2001 From: Arthur Date: Thu, 24 Oct 2019 14:46:16 +0800 Subject: [PATCH] fix: Context.Bind --- pkg/net/http/blademaster/context.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/net/http/blademaster/context.go b/pkg/net/http/blademaster/context.go index d9957b19e..c545f8af5 100644 --- a/pkg/net/http/blademaster/context.go +++ b/pkg/net/http/blademaster/context.go @@ -276,9 +276,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.