diff --git a/_examples/README_ZH.md b/_examples/README_ZH.md index 93cf7b5d..865557f8 100644 --- a/_examples/README_ZH.md +++ b/_examples/README_ZH.md @@ -227,7 +227,7 @@ func (m *MyController) MyCustomHandler(id int64) string { return "MyCustomHandle 如果是 `mvc.New(app.Party("/assets")).Handle(new(file.Controller))` -- `func(*Controller) GetByWildard(path string)` - `GET:/assets/{param:path}` +- `func(*Controller) GetByWildcard(path string)` - `GET:/assets/{param:path}` 支持的函数接收者类型是:int, int64, bool and string。 diff --git a/hero/binding.go b/hero/binding.go index 9e50871c..68ceba0a 100644 --- a/hero/binding.go +++ b/hero/binding.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" "sort" + "strings" "github.com/kataras/iris/v12/context" ) @@ -324,24 +325,33 @@ func payloadBinding(index int, typ reflect.Type) *binding { newValue = reflect.New(indirectType(input.Type)) ptr := newValue.Interface() - switch ctx.GetContentTypeRequested() { + contentType := ctx.GetContentTypeRequested() + if contentType != "" { + if idx := strings.IndexByte(contentType, ';'); idx > 0 { + // e.g. contentType=[multipart/form-data] trailing: ; boundary=4e2946168dbbac + contentType = contentType[:idx] + } + } + + switch contentType { case context.ContentXMLHeaderValue: err = ctx.ReadXML(ptr) case context.ContentYAMLHeaderValue: err = ctx.ReadYAML(ptr) - case context.ContentFormHeaderValue: - err = ctx.ReadQuery(ptr) - case context.ContentFormMultipartHeaderValue: + case context.ContentFormHeaderValue, context.ContentFormMultipartHeaderValue: err = ctx.ReadForm(ptr) - default: + case context.ContentJSONHeaderValue: err = ctx.ReadJSON(ptr) - // json + default: + if ctx.Request().URL.RawQuery != "" { + // try read from query. + err = ctx.ReadQuery(ptr) + } else { + // otherwise default to JSON. + err = ctx.ReadJSON(ptr) + } } - // if err != nil { - // return emptyValue, err - // } - if !wasPtr { newValue = newValue.Elem() } diff --git a/hero/handler_test.go b/hero/handler_test.go index 439978df..adc2c664 100644 --- a/hero/handler_test.go +++ b/hero/handler_test.go @@ -11,8 +11,8 @@ import ( // dynamic func type testUserStruct struct { - ID int64 `json:"id"` - Username string `json:"username"` + ID int64 `json:"id" form:"id" url:"id"` + Username string `json:"username" form:"username" url:"username"` } func testBinderFunc(ctx iris.Context) testUserStruct { @@ -142,8 +142,18 @@ func TestPayloadBinding(t *testing.T) { app.Post("/2", postHandler2) e := httptest.New(t, app) + + // JSON e.POST("/").WithJSON(iris.Map{"username": "makis"}).Expect().Status(httptest.StatusOK).Body().Equal("makis") e.POST("/2").WithJSON(iris.Map{"username": "kataras"}).Expect().Status(httptest.StatusOK).Body().Equal("kataras") + + // FORM (url-encoded) + e.POST("/").WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis") + // FORM (multipart) + e.POST("/").WithMultipart().WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis") + + // URL query. + e.POST("/").WithQuery("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis") } /* Author's notes: