diff --git a/pkg/flags/getters.go b/pkg/flags/getters.go index 72e5a3812..8165a0906 100644 --- a/pkg/flags/getters.go +++ b/pkg/flags/getters.go @@ -57,6 +57,15 @@ func WithQueryParameters(cmd *cobra.Command, query *QueryTemplate, inputIterator query.SetVariable(key, url.EscapeQueryString(val)) } } + case map[string][]string: + // Collect values + for key, values := range v { + encodedValues := make([]string, 0, len(values)) + for _, value := range values { + encodedValues = append(encodedValues, url.EscapeQueryString(value)) + } + query.SetVariable(key, encodedValues) + } case AnyString: query.SetVariable(name, string(v)) default: @@ -483,7 +492,9 @@ func WithCustomStringSlice(valuesFunc func() ([]string, error), opts ...string) dst = "" } - outputValues := make(map[string]string) + // Support setting multiple values for query parameters + // e.g. foo=bar1&foo=bar2 + outputValues := make(map[string][]string) for _, v := range values { parts := strings.Split(v, ":") if len(parts) != 2 { @@ -492,7 +503,12 @@ func WithCustomStringSlice(valuesFunc func() ([]string, error), opts ...string) continue } } - outputValues[strings.TrimSpace(parts[0])] = strings.TrimSpace(applyFormatter(format, parts[1])) + key := strings.TrimSpace(parts[0]) + value := strings.TrimSpace(applyFormatter(format, parts[1])) + if _, ok := outputValues[key]; !ok { + outputValues[key] = make([]string, 0, 1) + } + outputValues[key] = append(outputValues[key], value) } return dst, outputValues, err diff --git a/tests/manual/api/api.yaml b/tests/manual/api/api.yaml index 274e192fb..1da6c3fe1 100644 --- a/tests/manual/api/api.yaml +++ b/tests/manual/api/api.yaml @@ -140,6 +140,16 @@ tests: path: /inventory/managedObjects query: myValue=2&pageSize=1 + It adds multiple custom query parameters to an outgoing request with the same key: + command: | + echo "/inventory/managedObjects?pageSize=1" | + c8y api --customQueryParam "myValue=2" --customQueryParam "myValue=3" --customQueryParam other=value + stdout: + json: + method: GET + path: /inventory/managedObjects + query: myValue=2&myValue=3&other=value&pageSize=1 + It accepts positional arguments for method and path (not using pipeline): command: | c8y api GET "/alarm/alarms?pageSize=10&status=ACTIVE"