Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 0a9cc41

Browse files
committed
passing params as files for multipart/form-data type APIs needed some work
1 parent cd078fb commit 0a9cc41

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

CloudFlare/cloudflare.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,21 @@ def _add_headers(self, method, content_type, data, files):
103103
files = {}
104104
for k,v in data.items():
105105
# files[k] = (None, v, 'application/json')
106-
files[k] = (None, json.dumps(v), 'application/json')
106+
if isinstance(v, (dict, list)):
107+
files[k] = (None, json.dumps(v), 'application/json')
108+
else:
109+
files[k] = (None, v)
110+
# we have replaced data's values into files
107111
data = None
112+
if data is None and files is None and self.headers['Content-Type'] == 'multipart/form-data':
113+
# can't have zero length multipart/form-data and as there's no data or files; we don't need it
114+
del self.headers['Content-Type']
108115
if files:
109116
# overwrite Content-Type as we are uploading data
110117
self.headers['Content-Type'] = 'multipart/form-data'
111118
# however something isn't right and this works ... look at again later!
112119
del self.headers['Content-Type']
120+
return data, files
113121

114122
def _add_auth_headers(self, method):
115123
""" Add authentication headers """
@@ -181,7 +189,7 @@ def do_not_available(self, method, parts, identifiers, params=None, data=None, c
181189
def do_no_auth(self, method, parts, identifiers, params=None, data=None, content_type=None, files=None):
182190
""" Cloudflare v4 API"""
183191

184-
self._add_headers(method, content_type, data, files)
192+
data, files = self._add_headers(method, content_type, data, files)
185193
# We decide at this point if we are sending json or string data
186194
if isinstance(data, (str,bytes,bytearray)):
187195
return self._call(method, parts, identifiers, params, data, None, files)
@@ -190,7 +198,7 @@ def do_no_auth(self, method, parts, identifiers, params=None, data=None, content
190198
def do_auth(self, method, parts, identifiers, params=None, data=None, content_type=None, files=None):
191199
""" Cloudflare v4 API"""
192200

193-
self._add_headers(method, content_type, data, files)
201+
data, files = self._add_headers(method, content_type, data, files)
194202
self._add_auth_headers(method)
195203
# We decide at this point if we are sending json or string data
196204
if isinstance(data, (str,bytes,bytearray)):
@@ -200,7 +208,7 @@ def do_auth(self, method, parts, identifiers, params=None, data=None, content_ty
200208
def do_auth_unwrapped(self, method, parts, identifiers, params=None, data=None, content_type=None, files=None):
201209
""" Cloudflare v4 API"""
202210

203-
self._add_headers(method, content_type, data, files)
211+
data, files = self._add_headers(method, content_type, data, files)
204212
self._add_auth_headers(method)
205213
# We decide at this point if we are sending json or string data
206214
if isinstance(data, (str,bytes,bytearray)):
@@ -210,7 +218,7 @@ def do_auth_unwrapped(self, method, parts, identifiers, params=None, data=None,
210218
def do_certauth(self, method, parts, identifiers, params=None, data=None, content_type=None, files=None):
211219
""" Cloudflare v4 API"""
212220

213-
self._add_headers(method, content_type, data, files)
221+
data, files = self._add_headers(method, content_type, data, files)
214222
self._add_certtoken_headers(method)
215223
# We decide at this point if we are sending json or string data
216224
if isinstance(data, (str,bytes,bytearray)):
@@ -258,21 +266,12 @@ def _call_network(self, method, headers, parts, identifiers, params, data_str, d
258266
if len(parts) > 4 and parts[4]:
259267
url += '/' + parts[4]
260268

261-
if files and data_json:
262-
# Can't send data_json and form data - so move data_json into files and send as multipart/form-data
263-
new_files = []
264-
new_files += [(f, (files[f].name, files[f])) for f in files]
265-
new_files += [(d, (None, data_json[d])) for d in data_json]
266-
files = tuple(new_files)
267-
data_json = None
268-
269269
if self.logger:
270270
msg = build_curl(method, url, headers, params, data_str, data_json, files)
271271
self.logger.debug('Call: emulated curl command ...\n%s', msg)
272272

273273
try:
274274
response = self.network(method, url, headers, params, data_str, data_json, files)
275-
276275
except requests_ConnectionError as e:
277276
if self.logger:
278277
self.logger.debug('Call: requests connection exception! "%s"', e)

0 commit comments

Comments
 (0)