diff --git a/cli/cmd/api/api.go b/cli/cmd/api/api.go index d86d665130f..628792fe0bd 100644 --- a/cli/cmd/api/api.go +++ b/cli/cmd/api/api.go @@ -417,7 +417,7 @@ func emitRawBody(body []byte, fopts *cmdutil.FormatOptions) error { // digits through instead. decoder := json.NewDecoder(bytes.NewReader(body)) decoder.UseNumber() - if err := decoder.Decode(&bodyAny); err != nil { + if err := decoder.Decode(&bodyAny); err != nil || !json.Valid(body) { bodyAny = string(body) } } diff --git a/cli/cmd/api/api_test.go b/cli/cmd/api/api_test.go index 7b0fdb4f547..f9abd8b8018 100644 --- a/cli/cmd/api/api_test.go +++ b/cli/cmd/api/api_test.go @@ -719,3 +719,21 @@ func TestFieldBodyPreservesUnsignedID(t *testing.T) { t.Errorf("unexpected request: %s", body) } } + +func TestRawEnvelopePreservesNDJSON(t *testing.T) { + out, _ := iostreams.SetForTest(t) + body := []byte("{\"page\":1}\n{\"page\":2}\n") + opts := &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON} + if err := emitRawBody(body, opts); err != nil { + t.Fatal(err) + } + var envelope struct { + Data string `json:"data"` + } + if err := json.Unmarshal(out.Bytes(), &envelope); err != nil { + t.Fatal(err) + } + if envelope.Data != string(body) { + t.Errorf("response lost bytes: %q", envelope.Data) + } +}