WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@dwisiswant0
Copy link
Member

This commit introduces SetBody* methods to the
Request struct. These methods provide a
consistent and safe way to set request bodies,
ensuring that GetBody is correctly populated for
retries and redirects (307/308).

Changes:

  • SetBodyReader: Low-level setter for
    pre-existing reusable readers.
  • SetBody: Sets body from bytes, creating a
    fresh reusable reader for each attempt.
  • SetBodyString: Wrapper around SetBody for
    strings.
  • SetBodyStream: Consumes an io.Reader
    immediately into a reusable reader, handling
    io.Closer and ContentLength logic.
  • Updated TestRedirectPOST* tests to verify the
    new methods.

This addresses issues where retries or redirects
would fail due to a missing or non-reusable body.

Closes #495
Fixes #493

When sending POST requests with a non-empty body
that trigger 307/308 redirects, the redirects fail
because the underlying `http.Request.GetBody` is
nil by default.

This patch populates `GetBody` using the existing
reusable body reader, allowing the `net/http`
client to rewind and replay the body.

Fixes #493.

Signed-off-by: Dwi Siswanto <[email protected]>
Signed-off-by: Dwi Siswanto <[email protected]>
This commit introduces `SetBody*` methods to the
`Request` struct. These methods provide a
consistent and safe way to set request bodies,
ensuring that `GetBody` is correctly populated for
retries and redirects (307/308).

Changes:

* `SetBodyReader`: Low-level setter for
  pre-existing reusable readers.
* `SetBody`: Sets body from bytes, creating a
  fresh reusable reader for each attempt.
* `SetBodyString`: Wrapper around SetBody for
  strings.
* `SetBodyStream`: Consumes an `io.Reader`
  immediately into a reusable reader, handling
  `io.Closer` and `ContentLength` logic.
* Updated `TestRedirectPOST*` tests to verify the
  new methods.

This addresses issues where retries or redirects
would fail due to a missing or non-reusable body.

Signed-off-by: Dwi Siswanto <[email protected]>
@Mzack9999 Mzack9999 merged commit 772b3f4 into main Dec 6, 2025
7 checks passed
@Mzack9999 Mzack9999 deleted the dwisiswant0/feat/add-SetBody-methods-for-body-handling branch December 6, 2025 07:12
@Mzack9999 Mzack9999 added the Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. label Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add SetBody* methods to Request for reliable retries and redirects 307/308 Redirects Fail for POST Requests with Non-Empty Body (Missing req.GetBody)

3 participants