From 7033021637969b4430e5c998afb7e49899d6f80f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 3 Jun 2020 16:00:52 +0200 Subject: [PATCH] gh api: fix passing file/stdin contents via field arguments Reading from file via `-F foo=@myfile.txt` syntax would result in `[]byte` Go type, which by default gets serialized to JSON in base64 format, which we don't want here. Traverse all parameters and convert any `[]byte` into `string` before JSON serialization. --- pkg/cmd/api/http.go | 6 ++++++ pkg/cmd/api/http_test.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/api/http.go b/pkg/cmd/api/http.go index 5f9ebc3ca..812b95af4 100644 --- a/pkg/cmd/api/http.go +++ b/pkg/cmd/api/http.go @@ -22,6 +22,12 @@ func httpRequest(client *http.Client, method string, p string, params interface{ if strings.EqualFold(method, "GET") { url = addQuery(url, pp) } else { + for key, value := range pp { + switch vv := value.(type) { + case []byte: + pp[key] = string(vv) + } + } if isGraphQL { pp = groupGraphQLVariables(pp) } diff --git a/pkg/cmd/api/http_test.go b/pkg/cmd/api/http_test.go index e2ba8680b..0f9471111 100644 --- a/pkg/cmd/api/http_test.go +++ b/pkg/cmd/api/http_test.go @@ -157,7 +157,7 @@ func Test_httpRequest(t *testing.T) { method: "POST", p: "graphql", params: map[string]interface{}{ - "a": "b", + "a": []byte("b"), }, headers: []string{}, },