Fix request body handling in DoExponentialBackoff · Teamwork/utils@067a464 · GitHub
Skip to content

Commit 067a464

Browse files
committed
Fix request body handling in DoExponentialBackoff
Previously, DoExponentialBackoff relied on Seek to reset request bodies between retries. This approach failed when bodies didn't implement Seek or when req.GetBody wasn't set (e.g., manually wrapped io.NopCloser), causing "ContentLength=N with Body length 0" errors on retry attempts. The issue occurred because req.Clone() reuses the exhausted body reader without resetting it, leading to empty bodies on subsequent attempts while ContentLength remained set from the original request. Now read the entire request body upfront and create fresh io.Readers for each retry attempt. This ensures: - Body is always available for all retry attempts - ContentLength is correctly set to match the actual body - Works reliably with all io.Reader types, not just seekable ones Added test case with io.NopCloser-wrapped bytes.Buffer that validates both body content and ContentLength matching across retry attempts.
1 parent 929cb93 commit 067a464

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

‎httputilx/httputilx.go‎

Lines changed: 19 additions & 7 deletions

‎httputilx/httputilx_test.go‎

Lines changed: 51 additions & 1 deletion

0 commit comments

Comments
 (0)