Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def prepare_body(
if not isinstance(body, bytes):
body = body.encode("utf-8")

if isinstance(data, Iterable) and not isinstance(
if (isinstance(data, Iterable) or hasattr(data, "__iter__")) and not isinstance(
Comment thread
k223kim marked this conversation as resolved.
Outdated
data, (str, bytes, list, tuple, Mapping)
):
try:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,21 @@ def __iter__(self):

assert "Unable to rewind request body" in str(e)

def test_getattr_proxy_stream_follows_redirect(self, httpbin):
"""Ensure stream wrappers that don't implement __iter__ directly are still detected."""

class AttrProxy:
def __init__(self):
self._file = io.BytesIO(b"data")

def __getattr__(self, name):
return getattr(self._file, name)

r = requests.post(
httpbin("redirect-to?url=/post&status_code=307"), data=AttrProxy()
)
assert r.json()["data"] == "data"

def _patch_adapter_gzipped_redirect(self, session, url):
adapter = session.get_adapter(url=url)
org_build_response = adapter.build_response
Expand Down
Loading