POST Multiple Multipart-Encoded Files

You can send multiple files in one request. For example, suppose you want to upload image files to an HTML form with a multiple file field ‘images’:

  1. <input type="file" name="images" multiple="true" required="true"/>

To do that, just set files to a list of tuples of (form_field_name, file_info):

  1. >>> url = 'http://httpbin.org/post'
  2. >>> multiple_files = [
  3. ('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
  4. ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
  5. >>> r = requests.post(url, files=multiple_files)
  6. >>> r.text
  7. {
  8. ...
  9. 'files': {'images': 'data:image/png;base64,iVBORw ....'}
  10. 'Content-Type': 'multipart/form-data; boundary=3131623adb2043caaeb5538cc7aa0b3a',
  11. ...
  12. }

Warning

It is strongly recommended that you open files in binary mode. This is because Requests may attempt to provide the Content-Length header for you, and if it does this value will be set to the number of bytes in the file. Errors may occur if you open the file in text mode.