diff --git a/src/requests/models.py b/src/requests/models.py index c4b25fa079..8e190db319 100644 --- a/src/requests/models.py +++ b/src/requests/models.py @@ -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 diff --git a/tests/test_requests.py b/tests/test_requests.py index 75d2deff2e..e2e72b91ab 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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