chab
chab

Reputation: 23

Python requests unicode error

I'm using a simple Python script to POST files to a PHP script:

...
url = "http://example.com/upload.php"
r = requests.post(url, data=data, files=files)
...

I need to catch the response text, which is stored in

r.text

But when the response contains ASCII characters (e.g. an image file), the Python fails with this error:

content = str(self.content, encoding, errors='replace')
TypeError: unicode() argument 2 must be string, not None

Is there any way to avoid this error?

Upvotes: 2

Views: 4520

Answers (1)

jfs
jfs

Reputation: 414215

Binary file such as an image has no associated character encoding. Use r.content instead of r.text.

Upvotes: 8

Related Questions