Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion src/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ def _encode_files(

if isinstance(fp, (str, bytes, bytearray)):
fdata = fp
elif isinstance(fp, _SupportsRead): # defensive check for untyped callers
# data that proxies attributes to underlying objects needs hasattr
# defensive check for untyped callers
elif isinstance(fp, _SupportsRead) or hasattr(fp, "read"):
fdata = fp.read()
elif fp is None: # defensive check for untyped callers
continue
Expand Down
11 changes: 11 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,17 @@ def test_different_encodings_dont_break_post(self, httpbin):
)
assert r.status_code == 200

def test_post_named_tempfile(self, httpbin):
with tempfile.NamedTemporaryFile(mode="w+") as f:
f.write("named temp file contents\n")
f.seek(0)
r = requests.post(
httpbin("post"),
files={"file": f},
)
assert r.status_code == 200
assert r.json()["files"]["file"] == "named temp file contents\n"

@pytest.mark.parametrize(
"data",
(
Expand Down
Loading