Skip to content

Commit c0ca20c

Browse files
fix(assets): replace() use save/restore for Content-Type header
_api_client._call_request merges self.headers into every request via headers.update(self.headers), which overwrites any per-request copy before the request is sent. The only effective way to suppress Content-Type for multipart uploads is to temporarily pop it from the shared dict and restore it in a finally block. Also retains the with-open() fix so the file handle is always closed after the request.
1 parent 4c64ba7 commit c0ca20c

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

contentstack_management/assets/assets.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,17 @@ def replace(self, file_path):
174174
"""
175175

176176
url = f"assets/{self.asset_uid}"
177-
# Omit Content-Type so requests can set it with the correct multipart boundary.
178-
# Use a per-request header copy to avoid mutating the shared client headers dict.
179-
request_headers = {k: v for k, v in self.client.headers.items() if k.lower() != "content-type"}
180-
with open(file_path, 'rb') as fh:
181-
return self.client.put(url, headers=request_headers, params=self.params, files={"asset": fh})
177+
# _api_client._call_request does headers.update(self.headers) so a per-request
178+
# copy is overwritten before the request is sent. The only way to suppress
179+
# Content-Type is to temporarily remove it from the shared dict so requests can
180+
# set multipart/form-data with the correct boundary automatically.
181+
saved_ct = self.client.headers.pop("Content-Type", None)
182+
try:
183+
with open(file_path, 'rb') as fh:
184+
return self.client.put(url, headers=self.client.headers, params=self.params, files={"asset": fh})
185+
finally:
186+
if saved_ct is not None:
187+
self.client.headers["Content-Type"] = saved_ct
182188

183189
def generate(self, data):
184190
"""

0 commit comments

Comments
 (0)