Skip to content
Myr

encode files

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 31d10c0 · Browse files · 1 file+41 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+41 added−1 removed

  • other:+41 added−1 removed

1 file+41 added−1 removed

modified:requests/models.py+41 added-1 removed
102 unmodified lines
103 return urlencode(result, doseq=True)104 else:105 return data106107 @staticmethod108 def _encode_files(files, data):109 """Build the body for a multipart/form-data request.110111 Will successfully encode files when passed as a dict or a list of112 2-tuples. Order is retained if data is a list of 2-tuples but abritrary113 if parameters are supplied as a dict.114115 """116 if (not files) or isinstance(data, str):117 return None118119 new_fields = []120 fields = to_key_val_list(data)121 files = to_key_val_list(files)122123 for field, val in fields:124 if isinstance(val, list):125 for v in val:126 new_fields.append((field, builtin_str(v)))127 else:128 new_fields.append((field, builtin_str(val)))129130 for (k, v) in files:131 # support for explicit filename132 if isinstance(v, (tuple, list)):133 fn, fp = v134 else:135 fn = guess_filename(v) or k136 fp = v137 if isinstance(fp, str):138 fp = StringIO(fp)139 if isinstance(fp, bytes):140 fp = BytesIO(fp)141 new_fields.append((k, (fn, fp.read())))142143 body, content_type = encode_multipart_formdata(new_fields)144145 return body, content_type146147148class Request(object):149 """A user-created Request object."""150 def __init__(self,151 method=None,
155 unmodified lines
307 content_type = None308309 # Multi-part file uploads.310 if files:271 (body, content_type) = self._encode_files(files)311 (body, content_type) = self._encode_files(files, data)312 else:313 if data:314315 body = self._encode_params(data)
892 unmodified lines