diff --git a/AUTHORS.rst b/AUTHORS.rst index 907687d4de..c915e8513d 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -187,3 +187,4 @@ Patches and Suggestions - Nehal J Wani (`@nehaljwani `_) - Demetrios Bairaktaris (`@DemetriosBairaktaris `_) - Darren Dormer (`@ddormer `_) +- Rajiv Mayani (`@mayani `_) diff --git a/requests/models.py b/requests/models.py index ce4e284e64..37e7f9a25c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -155,8 +155,12 @@ def _encode_files(files, data): if isinstance(fp, (str, bytes, bytearray)): fdata = fp - else: + elif hasattr(fp, 'read'): fdata = fp.read() + elif fp is None: + continue + else: + fdata = fp rf = RequestField(name=k, data=fdata, filename=fn, headers=fh) rf.make_multipart(content_type=ft) diff --git a/tests/test_requests.py b/tests/test_requests.py index 0c7988dca3..0106713d50 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -676,6 +676,14 @@ def test_POSTBIN_GET_POST_FILES(self, httpbin): with pytest.raises(ValueError): requests.post(url, files=['bad file data']) + def test_invalid_files_input(self, httpbin): + + url = httpbin('post') + post = requests.post(url, + files={"random-file-1": None, "random-file-2": 1}) + assert b'name="random-file-1"' not in post.request.body + assert b'name="random-file-2"' in post.request.body + def test_POSTBIN_SEEKED_OBJECT_WITH_NO_ITER(self, httpbin): class TestStream(object):