Skip to content

Commit

Permalink
handle multipart/form-data correctly for more than one file and with …
Browse files Browse the repository at this point in the history
…params/data
  • Loading branch information
mahtin committed May 20, 2023
1 parent 13739c5 commit 36c40ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ def _call_network(self, method, headers, parts, identifiers, params, data, files
if parts[4]:
url += '/' + parts[4]

if files and data:
# Can't send data and form data - so move data into files and send as multipart/form-data
new_files = []
new_files += [(f, (files[f].name, files[f])) for f in files]
new_files += [(d, (None, data[d])) for d in data]
files = tuple(new_files)
data = None

if self.logger:
msg = build_curl(method, url, headers, params, data, files)
self.logger.debug('Call: emulated curl command ...\n%s', msg)
Expand Down
11 changes: 10 additions & 1 deletion CloudFlare/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ def build_curl(method, url, headers, params, data, files):
msg.append(' --data \'%s\' \\' % (str_data.replace('\n', ' ')))
# files
if files is not None:
msg.append(' --form "file=@%s" \\' % (files))
if isinstance(files, (list, tuple)):
for f in files:
if f[1][0] is None:
# not a file
msg.append(' --form "%s=%s" \\' % (f[0], f[1][1]))
else:
# a file
msg.append(' --form "%s=@%s" \\' % (f[0], f[1][0]))
else:
msg.append(' --form "file=@%s" \\' % (files))

# remove the last \ from the last line.
msg[-1] = msg[-1][:-1]
Expand Down

0 comments on commit 36c40ce

Please sign in to comment.