Skip to content

Conversation

IanButterworth
Copy link
Member

@IanButterworth IanButterworth commented Aug 25, 2025

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:

  • Redirect responses (3xx status codes) return false for iserror(res) but true for isredirect(res)
  • In readbody!(), they were going through the normal "success" path and writing their bodies to response_stream
  • When redirects are followed, these intermediate response bodies should be discarded, not written to the stream

Solution

Modified StreamRequest.jl to:

  1. Added willredirect() function that determines if a redirect response will actually be followed (using the same logic as RedirectRequest layer)
  2. Updated the condition in readbody!() from !iserror(res) to !iserror(res) && !willredirect(res)
  3. Redirect responses that will be followed are now treated like error responses - their bodies are read but stored in request context instead of written to response_stream

Behavior

  • Redirect followed: Only final response body written to response_stream
  • Multiple redirects: Only final response body written to response_stream
  • redirect=false: Redirect response body written to response_stream (correct)
  • Redirect limit reached: Final redirect response body written to response_stream (correct)
  • No redirects: Normal response body written to response_stream (unchanged)

Testing

Added comprehensive test case in client.jl that covers:

  • Basic redirect with response_stream
  • Multiple redirects with response_stream
  • Redirect disabled (redirect=false) with response_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.

@IanButterworth IanButterworth requested a review from quinnj August 25, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Response body for redirects not ignored in streaming mode
1 participant