Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow for explicit file content type support #1091

Merged
merged 2 commits into from

2 participants

Vinod Chandru Kenneth Reitz
Vinod Chandru

urllib3 allows for explicit content types to be specified for file data: https://github.com/kennethreitz/requests/blob/master/requests/packages/urllib3/filepost.py#L70

This patch allows that functionality to be exposed. A test is included.

Kenneth Reitz kennethreitz reopened this
Kenneth Reitz

Thanks!

Kenneth Reitz kennethreitz merged commit 6ec1a9c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 2 deletions.
  1. +1 −0  AUTHORS.rst
  2. +11 −2 requests/models.py
  3. +8 −0 test_requests.py
1  AUTHORS.rst
View
@@ -118,3 +118,4 @@ Patches and Suggestions
- Martijn Pieters
- Jonatan Heyman
- David Bonner <dbonner@gmail.com> @rascalking
+- Vinod Chandru
13 requests/models.py
View
@@ -108,8 +108,12 @@ 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
@@ -117,7 +121,12 @@ def _encode_files(files, data):
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)
8 test_requests.py
View
@@ -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(b"text/py-content-type" in r.request.body)
if __name__ == '__main__':
Something went wrong with that request. Please try again.