requests / toolbelt

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

Missing boundary in content-type leads to Attribute Error #303

Open ghost opened 3 years ago

ghost commented 3 years ago

Hi everybody,

we have a webservice which recently showed a 500 error. The root cause was a malformed multipart request.

Simplified example:

from requests_toolbelt.multipart.decoder import MultipartDecoder

body = b'--boundary xxx'
content_type = 'multipart/form-data'

for part in MultipartDecoder(body, content_type).parts:
    print(part)

Error: Traceback (most recent call last): File "repr.py", line 6, in <module> for part in MultipartDecoder(body, content_type).parts: File "/venv/lib/python3.7/site-packages/requests_toolbelt/multipart/decoder.py", line 111, in __init__ self._parse_body(content) File "/venv/lib/python3.7/site-packages/requests_toolbelt/multipart/decoder.py", line 137, in _parse_body boundary = b''.join((b'--', self.boundary)) AttributeError: 'MultipartDecoder' object has no attribute 'boundary'

As you see the boundary is missing in the content type. Would it be possible to detect this and raising a better error than AttributeError since AttributeError is very general and hard to catch. Thanks a lot.