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)
| 206 | yield from fields.items() |
| 207 | |
| 208 | def encode_multipart_formdata(fields, boundary=None): |
| 209 | """ |
| 210 | Encode a dictionary of ``fields`` using the multipart/form-data mime format. |
| 211 | |
| 212 | :param fields: |
| 213 | Dictionary of fields or list of (key, value) field tuples. The key is |
| 214 | treated as the field name, and the value as the body of the form-data |
| 215 | bytes. If the value is a tuple of two elements, then the first element |
| 216 | is treated as the filename of the form-data section. |
| 217 | |
| 218 | Field names and filenames must be str. |
| 219 | |
| 220 | :param boundary: |
| 221 | If not specified, then a random boundary will be generated using |
| 222 | :func:`mimetools.choose_boundary`. |
| 223 | """ |
| 224 | # copy requests imports in here: |
| 225 | from io import BytesIO |
| 226 | from requests.packages.urllib3.filepost import ( |
| 227 | choose_boundary, writer, b, get_content_type |
| 228 | ) |
| 229 | body = BytesIO() |
| 230 | if boundary is None: |
| 231 | boundary = choose_boundary() |
| 232 | |
| 233 | for fieldname, value in iter_fields(fields): |
| 234 | body.write(b('--%s\r\n' % (boundary))) |
| 235 | |
| 236 | if isinstance(value, tuple): |
| 237 | filename, data = value |
| 238 | writer(body).write('Content-Disposition: form-data; name="%s"; ' |
| 239 | 'filename="%s"\r\n' % (fieldname, filename)) |
| 240 | body.write(b('Content-Type: %s\r\n\r\n' % |
| 241 | (get_content_type(filename)))) |
| 242 | else: |
| 243 | data = value |
| 244 | writer(body).write('Content-Disposition: form-data; name="%s"\r\n' |
| 245 | % (fieldname)) |
| 246 | body.write(b'Content-Type: text/plain\r\n\r\n') |
| 247 | |
| 248 | if isinstance(data, int): |
| 249 | data = str(data) # Backwards compatibility |
| 250 | if isinstance(data, str): |
| 251 | writer(body).write(data) |
| 252 | else: |
| 253 | body.write(data) |
| 254 | |
| 255 | body.write(b'\r\n') |
| 256 | |
| 257 | body.write(b('--%s--\r\n' % (boundary))) |
| 258 | |
| 259 | content_type = b('multipart/form-data; boundary=%s' % boundary) |
| 260 | |
| 261 | return body.getvalue(), content_type |
| 262 | |
| 263 | |
| 264 | def post_download(project, filename, name=None, description=""): |
no test coverage detected
searching dependent graphs…