requests / toolbelt

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

MultipartEncoder int encoding issues #39

Closed fl33t closed 9 years ago

fl33t commented 10 years ago

Hello,

The MultipartEncoder appears to not properly handle int's passed to the encoder.

Example payload:

multipart_payload = MultipartEncoder(
                fields={
                    'test': 1,
                    'Filedata': (
                        filename,
                        filecontent,
                        'application/xml'
                    ),
                }
            )

An integer passed to the Multipart Encoder results in:

  File "[REMOVED]/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py", line 89, in __init__
    self._prepare_parts()
  File "[REMOVED]/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py", line 171, in _prepare_parts
    self.parts = [Part.from_field(f, enc) for f in fields]
  File "[REMOVED]/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py", line 171, in <listcomp>
    self.parts = [Part.from_field(f, enc) for f in fields]
  File "[REMOVED]/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py", line 388, in from_field
    body = coerce_data(field.data, encoding)
  File "[REMOVED]/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py", line 364, in coerce_data
    return CustomBytesIO(data, encoding)
  File "[REMOVED]/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py", line 429, in __init__
    buffer = encode_with(buffer, encoding)
  File "[REMOVED]/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py", line 326, in encode_with
    return string.encode(item, encoding)
AttributeError: 'int' object has no attribute 'encode'

A temporary workaround is to force the int to string via prepending zeroes.

jxa

sigmavirus24 commented 10 years ago

Every value should be a bytestring (or bytearray, or memoryview). We do not (and will not) coerce your data for you.