Do not try to parse bodies for HEAD requests

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
This commit is contained in:
Josh Soref 2025-02-07 10:24:27 -05:00
parent b77778291f
commit 023e347078
2 changed files with 34 additions and 3 deletions

View file

@ -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
}
}
}

View file

@ -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