diff --git a/server/web/controller.go b/server/web/controller.go index eb8b9c68..9720ac4b 100644 --- a/server/web/controller.go +++ b/server/web/controller.go @@ -151,12 +151,6 @@ type ControllerInterface interface { CheckXSRFCookie() bool HandlerFunc(fn string) bool URLMapping() - Bind(obj interface{}) error - BindJson(obj interface{}) error - BindXML(obj interface{}) error - BindForm(obj interface{}) error - BindProtobuf(obj interface{}) error - BindYAML(obj interface{}) error } // Init generates default values of controller operations. @@ -250,7 +244,10 @@ func (c *Controller) HandlerFunc(fnname string) bool { func (c *Controller) URLMapping() {} func (c *Controller) Bind(obj interface{}) error { - ct := c.Ctx.Request.Header["Content-Type"] + ct, exist := c.Ctx.Request.Header["Content-Type"] + if exist == false || len(ct) == 0 { + return c.BindJson(obj) + } i, l := 0, len(ct[0]) for ; i < l && ct[0][i] != ';'; i++ { } diff --git a/server/web/controller_test.go b/server/web/controller_test.go index f3f5f321..1f82698e 100644 --- a/server/web/controller_test.go +++ b/server/web/controller_test.go @@ -198,6 +198,21 @@ func TestBindJson(t *testing.T) { assert.Equal(t, "FOO", s.Foo) } +func TestBindNoContentType(t *testing.T) { + var s struct { + Foo string `json:"foo"` + } + header := map[string][]string{} + request := &http.Request{Header: header} + input := &context.BeegoInput{RequestBody: []byte(`{"foo": "FOO"}`)} + ctx := &context.Context{Request: request, Input: input} + ctrlr := Controller{Ctx: ctx} + err := ctrlr.Bind(&s) + require.NoError(t, err) + assert.Equal(t, "FOO", s.Foo) +} + + func TestBindXML(t *testing.T) { var s struct {