Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: io.Reader can only be read once (#3332)

p0ny7f29599091e2
diff --git a/plugin/vcs/internal/oauth/oauth.go b/plugin/vcs/internal/oauth/oauth.go
index 6da6b6f302..7a6c9015af 100644
--- a/plugin/vcs/internal/oauth/oauth.go
+++ b/plugin/vcs/internal/oauth/oauth.go
@@ -2,6 +2,7 @@
package oauth
import (
+ "bytes"
"context"
"encoding/json"
"fmt"
@@ -17,8 +18,20 @@ import (
type TokenRefresher func(ctx context.Context, client *http.Client, oldToken *string) error
func requester(ctx context.Context, client *http.Client, method, url string, token *string, body io.Reader) func() (*http.Response, error) {
+ // The body may be read multiple times but io.Reader is meant to be read once,
+ // so we read the body first and build the reader every time.
+ var bodyBytes []byte
+ if body != nil {
+ b, err := io.ReadAll(body)
+ if err != nil {
+ return func() (*http.Response, error) {
+ return nil, errors.Wrap(err, "failed to read from body")
+ }
+ }
+ bodyBytes = b
+ }
return func() (*http.Response, error) {
- req, err := http.NewRequestWithContext(ctx, method, url, body)
+ req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(bodyBytes))
if err != nil {
return nil, errors.Wrapf(err, "construct %s %s", method, url)
}