2

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?

2
  • Not handing in None as second argument (which seems to be encoding)? (i.e., it might be useful to have information where enconding comes from) Commented Jun 26, 2012 at 15:43
  • Post more info, a call stack perhaps? Commented Jun 26, 2012 at 15:45

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.