Skip to content
Myr

Merge pull request #1091 from vinodc/specify_file_content_type

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Allow for explicit file content type support

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 4a5b5bc, fbc366c · Browse files · 3 files+20 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+20 added−2 removed

  • other:+20 added−2 removed

3 files+20 added−2 removed

modified:requests/models.py+11 added-2 removed
106 unmodified lines
107 new_fields.append((field, builtin_str(val)))108109 for (k, v) in files:110 # support for explicit filename111 ft = None112 if isinstance(v, (tuple, list)):113 if len(v) == 2:112 fn, fp = v114 fn, fp = v115 else:116 fn, fp, ft = v117 else:118 fn = guess_filename(v) or k119 fp = v120 if isinstance(fp, str):121 fp = StringIO(fp)122 if isinstance(fp, bytes):123 fp = BytesIO(fp)124 125 if ft:126 new_v = (fn, fp.read(), ft)127 else:128 new_v = (fn, fp.read())120 new_fields.append((k, (fn, fp.read())))129 new_fields.append((k, new_v))130131 body, content_type = encode_multipart_formdata(new_fields)132133 return body, content_type
483 unmodified lines
modified:AUTHORS.rst+1 added
116 unmodified lines
117- Stephen Zhuang (everbird)118- Martijn Pieters119- Jonatan Heyman120- David Bonner <dbonner@gmail.com> @rascalking121- Vinod Chandru
modified:test_requests.py+8 added
253 unmodified lines
254 params={'blah': 'asdf1234'},255 files={'file': ('test_requests.py', open(__file__, 'rb'))})256 self.assertEqual(r.status_code, 200)257258 def test_custom_content_type(self):259 r = requests.post(httpbin('post'),260 data={'stuff': json.dumps({'a': 123})},261 files={'file1': ('test_requests.py', open(__file__, 'rb')),262 'file2': ('test_requests', open(__file__, 'rb'),263 'text/py-content-type')})264 self.assertEqual(r.status_code, 200)265 self.assertTrue(b"text/py-content-type" in r.request.body)266267268if __name__ == '__main__':269 unittest.main()