added support for explicit filenames in `files` parameter.
authorRolando Espinoza La fuente <darkrho@gmail.com>
Wed, 16 Nov 2011 04:03:19 +0000 (00:03 -0400)
committerRolando Espinoza La fuente <darkrho@gmail.com>
Wed, 16 Nov 2011 04:03:19 +0000 (00:03 -0400)
files = {
    "files[]": ("myfile", open("foo")),
}

requests/models.py

index 97237e7..75b9856 100644 (file)
@@ -371,7 +371,13 @@ class Request(object):
                     fields = dict(self.data)
 
                 for (k, v) in self.files.items():
-                    fields.update({k: (guess_filename(k) or k, v.read())})
+                    # support for explicit filename
+                    if isinstance(v, (tuple, list)):
+                        fn, fp = v
+                    else:
+                        fn = guess_filename(v) or k
+                        fp = v
+                    fields.update({k: (fn, fp.read())})
 
                 (body, content_type) = encode_multipart_formdata(fields)
             else: