Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust check for upload files in binary mode #2630

Merged
merged 7 commits into from
Apr 20, 2023
8 changes: 4 additions & 4 deletions httpx/_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ def __init__(self, name: str, value: FileTypes) -> None:
# requests does the opposite (it overwrites the header with the 3rd tuple element)
headers["Content-Type"] = content_type

if "b" not in getattr(fileobj, "mode", "b"):
raise TypeError(
"Multipart file uploads must be opened in binary mode, not text mode."
)
if isinstance(fileobj, io.StringIO):
raise TypeError(
"Multipart file uploads require 'io.BytesIO', not 'io.StringIO'."
)
if isinstance(fileobj, io.TextIOBase):
raise TypeError(
"Multipart file uploads must be opened in binary mode, not text mode."
)

self.filename = filename
self.file = fileobj
Expand Down