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

RHCLOUD-36959 | fix: the API client panics sometimes with an empty body #114

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions sources/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,16 @@ func (sc *sourcesClient) sendRequest(ctx context.Context, httpMethod string, url
requestBody = bytes.NewBuffer(tmp)
}

// Create the request.
request, err := http.NewRequestWithContext(ctx, httpMethod, url.String(), requestBody)
// Create the request. Apparently a nil "*bytes.Buffer" counts as a body, which in turn makes the code panic when
// creating a new request. That is why we add another "if" statement to guard us against that.
var request *http.Request
var err error
if requestBody != nil {
request, err = http.NewRequestWithContext(ctx, httpMethod, url.String(), requestBody)
} else {
request, err = http.NewRequestWithContext(ctx, httpMethod, url.String(), nil)
}

if err != nil {
return fmt.Errorf(`failed to create request: %w`, err)
}
Expand Down
Loading