Skip to content
Open
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: 2 additions & 2 deletions src/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ def prepare_body(self, data, files, json=None):

self.prepare_content_length(body)

# Add content-type if it wasn't explicitly provided.
if content_type and ("content-type" not in self.headers):
# Add content-type if it wasn't explicitly provided, or it was explicitly unset
if content_type and self.headers.get("content-type") is None:
self.headers["Content-Type"] = content_type

self.body = body
Expand Down
12 changes: 12 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,18 @@ def test_can_send_objects_with_files(self, httpbin, files):
p = r.prepare()
assert "multipart/form-data" in p.headers["Content-Type"]

def test_can_override_session_content_type_with_multipart_request(self, httpbin):
s = requests.Session()
s.headers["Content-Type"] = "application/json"
data = {"a": "this is a string"}
files = {"b": "foo"}
headers = {"Content-Type": None}
r = requests.Request(
"POST", httpbin("post"), headers=headers, data=data, files=files
)
p = s.prepare_request(r)
assert "multipart/form-data" in p.headers["Content-Type"]

def test_can_send_file_object_with_non_string_filename(self, httpbin):
f = io.BytesIO()
f.name = 2
Expand Down