Skip to content

Commit

Permalink
The library raises NoneType error when file-pointer (fp) resolves to …
Browse files Browse the repository at this point in the history
…None.

>>> from requests import post
>>> r = post("https://example.com", files={"file-name": None})

However, when a param value or json field is None they are not included in the request body.

>>> from requests import get
>>> r = get("https://example.com", params={"file-name": None})
>>> r.request.url

This commit makes the beahviour consistent for files.
  • Loading branch information
mayani authored and nateprewitt committed May 17, 2018
1 parent 5f09f0c commit 8546a15
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,4 @@ Patches and Suggestions
- Nehal J Wani (`@nehaljwani <https://github.com/nehaljwani>`_)
- Demetrios Bairaktaris (`@DemetriosBairaktaris <https://github.com/demetriosbairaktaris>`_)
- Darren Dormer (`@ddormer <https://github.com/ddormer>`_)
- Rajiv Mayani (`@mayani <https://github.com/mayani>`_)
6 changes: 5 additions & 1 deletion requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 8546a15

Please sign in to comment.