requests / toolbelt

A toolbelt of useful classes and functions to be used with python-requests
https://toolbelt.readthedocs.org
Other
989 stars 186 forks source link

Sending a zero-part multipart document has an uncaught exception; `.parts` should be an empty list instead #345

Open dragon-dxw opened 1 year ago

dragon-dxw commented 1 year ago

If returning a list-like object, MarkLogic can return a multipart document, one part per returned value. For an empty list, it returns zero parts (i.e. no bytes). Requests Toolbelt crashes in this instance, because the assumption that a content type must exist at all is violated.

File /usr/local/lib/python3.9/site-packages/caselawclient/Client.py:29 in decode_multipart
multipart_data = decoder.MultipartDecoder.from_response(response)
File /usr/local/lib/python3.9/site-packages/requests_toolbelt/multipart/decoder.py:156 in from_response
return cls(content, content_type, encoding)
File /usr/local/lib/python3.9/site-packages/requests_toolbelt/multipart/decoder.py:110 in __init__
self._find_boundary()
File /usr/local/lib/python3.9/site-packages/requests_toolbelt/multipart/decoder.py:114 in _find_boundary
ct_info = tuple(x.strip() for x in self.content_type.split(';'))
AttributeError: 'NoneType' object has no attribute 'split'

We got around this by hardcoding that no content means zero items returned, but this feels like a thing worth solving at the library level. our workaround: https://github.com/nationalarchives/ds-caselaw-custom-api-client/pull/99/files

sigmavirus24 commented 1 year ago

Want to send a PR? I think it should be relatively simple to add this logic/handling