Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Jan 10, 2013
2 parents 41687c8 + 91f6e69 commit 1a7c91f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ Patches and Suggestions
- Martijn Pieters
- Jonatan Heyman
- David Bonner <[email protected]> @rascalking
- Vinod Chandru
13 changes: 11 additions & 2 deletions requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 13 additions & 2 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ def merge_kwargs(local_kwarg, default_kwarg):
default_kwarg = from_key_val_list(default_kwarg)
local_kwarg = from_key_val_list(local_kwarg)

# Update new values.
# Update new values in a case-insensitive way
def get_original_key(original_keys, new_key):
"""
Finds the key from original_keys that case-insensitive matches new_key.
"""
for original_key in original_keys:
if key.lower() == original_key.lower():
return original_key
return new_key

kwargs = default_kwarg.copy()
kwargs.update(local_kwarg)
original_keys = kwargs.keys()
for key, value in local_kwarg.items():
kwargs[get_original_key(original_keys, key)] = value

# Remove keys that are set to None.
for (k, v) in local_kwarg.items():
Expand Down
8 changes: 8 additions & 0 deletions test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit 1a7c91f

Please sign in to comment.