Fix redirect response bodies being written to response_stream #1232
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Note, written by Claude, hence the draft.
Fixes #1165
Problem
When using
response_stream
with HTTP requests that involve redirects, both the redirect response body AND the final response body were being written to the stream. This is incorrect behavior - only the final response body should be written to the stream, and redirect response bodies should be ignored (similar to how curl handles redirects with "Ignoring the response-body").Root Cause
The issue was introduced in the version range v1.5.5...v1.6.3, specifically related to changes in PR #975 that fixed StatusError exception handling. While that fix was correct and necessary, it highlighted an existing issue in StreamRequest.jl:
false
foriserror(res)
buttrue
forisredirect(res)
readbody!()
, they were going through the normal "success" path and writing their bodies toresponse_stream
Solution
Modified StreamRequest.jl to:
willredirect()
function that determines if a redirect response will actually be followed (using the same logic asRedirectRequest
layer)readbody!()
from!iserror(res)
to!iserror(res) && !willredirect(res)
response_stream
Behavior
response_stream
response_stream
redirect=false
: Redirect response body written toresponse_stream
(correct)response_stream
(correct)response_stream
(unchanged)Testing
Added comprehensive test case in client.jl that covers:
response_stream
response_stream
redirect=false
) withresponse_stream
The fix ensures HTTP.jl now matches the expected behavior described in the issue and aligns with how other HTTP clients (like curl) handle redirect response bodies in streaming mode.