diff --git a/rest/httpx/requests_test.go b/rest/httpx/requests_test.go index a1d3f6a8..3c4d8d16 100644 --- a/rest/httpx/requests_test.go +++ b/rest/httpx/requests_test.go @@ -16,17 +16,21 @@ import ( func TestParseForm(t *testing.T) { var v struct { - Name string `form:"name"` - Age int `form:"age"` - Percent float64 `form:"percent,optional"` + Name string `form:"name"` + Age int `form:"age"` + Percent float64 `form:"percent,optional"` + Statuses []string `form:"statuses[],optional"` } - r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4", http.NoBody) + r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4&statuses[]=try&statuses[]=done", http.NoBody) assert.Nil(t, err) assert.Nil(t, Parse(r, &v)) assert.Equal(t, "hello", v.Name) assert.Equal(t, 18, v.Age) assert.Equal(t, 3.4, v.Percent) + assert.Equal(t, 2, len(v.Statuses)) + assert.True(t, v.Statuses[0] == "try" || v.Statuses[1] == "try") + assert.True(t, v.Statuses[0] == "done" || v.Statuses[1] == "done") } func TestParseForm_Error(t *testing.T) { diff --git a/rest/httpx/util.go b/rest/httpx/util.go index 9623761a..9301522c 100644 --- a/rest/httpx/util.go +++ b/rest/httpx/util.go @@ -21,9 +21,10 @@ func GetFormValues(r *http.Request) (map[string]any, error) { params := make(map[string]any, len(r.Form)) for name := range r.Form { - formValue := r.Form.Get(name) - if len(formValue) > 0 { - params[name] = formValue + if len(r.Form[name]) > 1 { + params[name] = r.Form[name] + } else if len(r.Form[name]) == 1 { + params[name] = r.Form.Get(name) } }