diff --git a/util.go b/util.go index 4d4edd7..b0496c5 100644 --- a/util.go +++ b/util.go @@ -217,13 +217,21 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta _, _ = b.WriteString(values.Encode()) } - if rutil.IsZero(nmsg) { + if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) { return b.String(), nil, nil } return b.String(), nmsg, nil } +func isEmptyStruct(v interface{}) bool { + val := reflect.ValueOf(v) + if val.Kind() == reflect.Ptr { + val = val.Elem() + } + return val.Kind() == reflect.Struct && val.NumField() == 0 +} + func newTemplate(path string) ([]string, error) { if len(path) == 0 || path[0] != '/' { return nil, fmt.Errorf("path must starts with /") diff --git a/util_test.go b/util_test.go index 5d8b799..d1fb061 100644 --- a/util_test.go +++ b/util_test.go @@ -59,6 +59,30 @@ func TestNewPathRequest(t *testing.T) { } } +func TestNewPathRequestWithEmptyBody(t *testing.T) { + val := struct{}{} + + for _, m := range []string{"POST", "PUT", "PATCH", "GET", "DELETE"} { + body := `{"type": "invalid"}` + path, nmsg, err := newPathRequest("/v1/test", m, body, val, []string{"protobuf", "json"}, nil) + if err != nil { + t.Fatal(err) + } + if nmsg == nil { + t.Fatalf("invalid path: nil nmsg") + } + + u, err := url.Parse(path) + if err != nil { + t.Fatal(err) + } + vals := u.Query() + if len(vals) != 0 { + t.Fatalf("invalid path: %v nmsg: %v", path, nmsg) + } + } +} + func TestNewPathVarRequest(t *testing.T) { type Message struct { Name string `json:"name"`