Skip to content

Python-http.client codegens fails to run if non-text files are being uploaded in the request. #366

@webholik

Description

@webholik

The generated code tries to open the file as text without checking if its binary:

with open('file.jpeg') as f:
  dataList.append(f.read())

which leads to the following error:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    dataList.append(f.read())
  File "/usr/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Even if you manually change code to open the file as binary:

with open('file.jpeg', 'rb') as f:
  dataList.append(f.read())

The code still fails:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    body = '\r\n'.join(dataList)
TypeError: sequence item 4: expected str instance, bytes found

This is because the join method fails if the given list contains binary data.

Note: The bug only exists for Python 3 as http.client is Python 3 only.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions