MCPcopy Create free account
hub / github.com/ipython/ipython / encode_multipart_formdata

Function encode_multipart_formdata

tools/gh_api.py:218–271  ·  view source on GitHub ↗

Encode a dictionary of ``fields`` using the multipart/form-data mime format. :param fields: Dictionary of fields or list of (key, value) field tuples. The key is treated as the field name, and the value as the body of the form-data bytes. If the value is a tuple of

(fields, boundary=None)

Source from the content-addressed store, hash-verified

216 yield k,v
217
218def encode_multipart_formdata(fields, boundary=None):
219 """
220 Encode a dictionary of ``fields`` using the multipart/form-data mime format.
221
222 :param fields:
223 Dictionary of fields or list of (key, value) field tuples. The key is
224 treated as the field name, and the value as the body of the form-data
225 bytes. If the value is a tuple of two elements, then the first element
226 is treated as the filename of the form-data section.
227
228 Field names and filenames must be unicode.
229
230 :param boundary:
231 If not specified, then a random boundary will be generated using
232 :func:`mimetools.choose_boundary`.
233 """
234 # copy requests imports in here:
235 from io import BytesIO
236 from requests.packages.urllib3.filepost import (
237 choose_boundary, six, writer, b, get_content_type
238 )
239 body = BytesIO()
240 if boundary is None:
241 boundary = choose_boundary()
242
243 for fieldname, value in iter_fields(fields):
244 body.write(b('--%s\r\n' % (boundary)))
245
246 if isinstance(value, tuple):
247 filename, data = value
248 writer(body).write('Content-Disposition: form-data; name="%s"; '
249 'filename="%s"\r\n' % (fieldname, filename))
250 body.write(b('Content-Type: %s\r\n\r\n' %
251 (get_content_type(filename))))
252 else:
253 data = value
254 writer(body).write('Content-Disposition: form-data; name="%s"\r\n'
255 % (fieldname))
256 body.write(b'Content-Type: text/plain\r\n\r\n')
257
258 if isinstance(data, int):
259 data = str(data) # Backwards compatibility
260 if isinstance(data, six.text_type):
261 writer(body).write(data)
262 else:
263 body.write(data)
264
265 body.write(b'\r\n')
266
267 body.write(b('--%s--\r\n' % (boundary)))
268
269 content_type = b('multipart/form-data; boundary=%s' % boundary)
270
271 return body.getvalue(), content_type
272
273
274def post_download(project, filename, name=None, description=""):

Callers 1

post_downloadFunction · 0.85

Calls 2

iter_fieldsFunction · 0.85
writeMethod · 0.45

Tested by

no test coverage detected