Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid sending empty JSON body when no params to api command #6775

Merged
merged 2 commits into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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