From 2aa7a037b389b9fbf9ab05f9079552f4241bb577 Mon Sep 17 00:00:00 2001 From: Lucas Hinderberger Date: Tue, 9 Jul 2024 17:43:40 +0200 Subject: [PATCH] Adding support for array query params --- pkg/lib/api/request.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/pkg/lib/api/request.go b/pkg/lib/api/request.go index c2d04f4..eea8c98 100755 --- a/pkg/lib/api/request.go +++ b/pkg/lib/api/request.go @@ -10,6 +10,7 @@ import ( "net/http/httputil" "net/url" "path" + "reflect" "strings" "time" @@ -116,17 +117,34 @@ func (request Request) buildHttpRequest() (req *http.Request, err error) { q := req.URL.Query() - addToQuery := func(key string, v any) error { - stringVal, err := util.GetStringFromInterface(v) - if err != nil { - return fmt.Errorf("error GetStringFromInterface: %s", err) - } + addToQuery := func(key string, val any) error { + var vs []any + + refval := reflect.ValueOf(val) - if stringVal == "" { - return nil + switch refval.Kind() { + case reflect.Array, reflect.Slice: + l := refval.Len() + vs = make([]any, l) + for i := 0; i < l; i++ { + vs[i] = refval.Index(i).Interface() + } + default: + vs = []any{val} } - q.Add(key, stringVal) + for _, v := range vs { + stringVal, err := util.GetStringFromInterface(v) + if err != nil { + return fmt.Errorf("error GetStringFromInterface: %s", err) + } + + if stringVal == "" { + return nil + } + + q.Add(key, stringVal) + } return nil }