From 023e3470780c2bafca6b08e6eaada56eb8f7ad77 Mon Sep 17 00:00:00 2001 From: Josh Soref <2119212+jsoref@users.noreply.github.com> Date: Fri, 7 Feb 2025 10:24:27 -0500 Subject: [PATCH] Do not try to parse bodies for HEAD requests Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> --- pkg/cmd/api/api.go | 8 +++++--- pkg/cmd/api/api_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index a6eb69718..a1308d09f 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -463,9 +463,11 @@ func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersW var serverError string if isJSON && (opts.RequestPath == "graphql" || resp.StatusCode >= 400) { - responseBody, serverError, err = parseErrorResponse(responseBody, resp.StatusCode) - if err != nil { - return + if !strings.EqualFold(opts.RequestMethod, "HEAD") { + responseBody, serverError, err = parseErrorResponse(responseBody, resp.StatusCode) + if err != nil { + return + } } } diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index a565312cd..321f7b7c0 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -1234,6 +1234,35 @@ func Test_apiRun_DELETE(t *testing.T) { } } +func Test_apiRun_HEAD(t *testing.T) { + ios, _, _, _ := iostreams.Test() + + err := apiRun(&ApiOptions{ + IO: ios, + Config: func() (gh.Config, error) { + return config.NewBlankConfig(), nil + }, + HttpClient: func() (*http.Client, error) { + var tr roundTripper = func(req *http.Request) (*http.Response, error) { + return &http.Response{ + StatusCode: 422, + Request: req, + Header: map[string][]string{ + "Content-Type": {"application/json"}, + }}, nil + } + return &http.Client{Transport: tr}, nil + }, + MagicFields: []string(nil), + RawFields: []string(nil), + RequestMethod: "HEAD", + RequestMethodPassed: true, + }) + if err != cmdutil.SilentError { + t.Fatalf("got error %v", err) + } +} + func Test_apiRun_inputFile(t *testing.T) { tests := []struct { name string