Skip to content

Commit

Permalink
Avoid sending empty JSON body when no params to api command (#6775)
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Dec 22, 2022
1 parent c5c2f9c commit 6a55324
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/cmd/api/api.go
Expand Up @@ -246,7 +246,10 @@ func apiRun(opts *ApiOptions) error {
}
method := opts.RequestMethod
requestHeaders := opts.RequestHeaders
var requestBody interface{} = params
var requestBody interface{}
if len(params) > 0 {
requestBody = params
}

if !opts.RequestMethodPassed && (len(params) > 0 || opts.RequestInputFile != "") {
method = "POST"
Expand Down
32 changes: 32 additions & 0 deletions pkg/cmd/api/api_test.go
Expand Up @@ -671,6 +671,7 @@ func Test_apiRun_paginationGraphQL(t *testing.T) {
return config.NewBlankConfig(), nil
},

RawFields: []string{"foo=bar"},
RequestMethod: "POST",
RequestPath: "graphql",
Paginate: true,
Expand Down Expand Up @@ -764,6 +765,7 @@ func Test_apiRun_paginated_template(t *testing.T) {

RequestMethod: "POST",
RequestPath: "graphql",
RawFields: []string{"foo=bar"},
Paginate: true,
// test that templates executed per page properly render a table.
Template: `{{range .data.nodes}}{{tablerow .page .caption}}{{end}}`,
Expand Down Expand Up @@ -798,6 +800,36 @@ func Test_apiRun_paginated_template(t *testing.T) {
assert.Equal(t, "PAGE1_END", endCursor)
}

func Test_apiRun_DELETE(t *testing.T) {
ios, _, _, _ := iostreams.Test()

var gotRequest *http.Request
err := apiRun(&ApiOptions{
IO: ios,
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
HttpClient: func() (*http.Client, error) {
var tr roundTripper = func(req *http.Request) (*http.Response, error) {
gotRequest = req
return &http.Response{StatusCode: 204, Request: req}, nil
}
return &http.Client{Transport: tr}, nil
},
MagicFields: []string(nil),
RawFields: []string(nil),
RequestMethod: "DELETE",
RequestMethodPassed: true,
})
if err != nil {
t.Fatalf("got error %v", err)
}

if gotRequest.Body != nil {
t.Errorf("expected nil request body, got %T", gotRequest.Body)
}
}

func Test_apiRun_inputFile(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 6a55324

Please sign in to comment.