Browse files

Allow for third argument in file dict value to support explicit

file content type.
  • Loading branch information...
1 parent b450528 commit 20b10aed1bbe277745a74953b6dc73290bfa82fa @vinodc vinodc committed Jan 10, 2013
Showing with 20 additions and 2 deletions.
  1. +1 −0 AUTHORS.rst
  2. +11 −2 requests/models.py
  3. +8 −0 test_requests.py
View
1 AUTHORS.rst
@@ -118,3 +118,4 @@ Patches and Suggestions
- Martijn Pieters
- Jonatan Heyman
- David Bonner <dbonner@gmail.com> @rascalking
+- Vinod Chandru
View
13 requests/models.py
@@ -108,16 +108,25 @@ def _encode_files(files, data):
for (k, v) in files:
# support for explicit filename
+ ft = None
if isinstance(v, (tuple, list)):
- fn, fp = v
+ if len(v) == 2:
+ fn, fp = v
+ else:
+ fn, fp, ft = v
else:
fn = guess_filename(v) or k
fp = v
if isinstance(fp, str):
fp = StringIO(fp)
if isinstance(fp, bytes):
fp = BytesIO(fp)
- new_fields.append((k, (fn, fp.read())))
+
+ if ft:
+ new_v = (fn, fp.read(), ft)
+ else:
+ new_v = (fn, fp.read())
+ new_fields.append((k, new_v))
body, content_type = encode_multipart_formdata(new_fields)
View
8 test_requests.py
@@ -255,6 +255,14 @@ def test_different_encodings_dont_break_post(self):
files={'file': ('test_requests.py', open(__file__, 'rb'))})
self.assertEqual(r.status_code, 200)
+ def test_custom_content_type(self):
+ r = requests.post(httpbin('post'),
+ data={'stuff': json.dumps({'a': 123})},
+ files={'file1': ('test_requests.py', open(__file__, 'rb')),
+ 'file2': ('test_requests', open(__file__, 'rb'),
+ 'text/py-content-type')})
+ self.assertEqual(r.status_code, 200)
+ self.assertTrue("text/py-content-type" in r.request.body)
if __name__ == '__main__':

0 comments on commit 20b10ae

Please sign in to comment.