2

In my below code I am getting an error "raise TypeError("POST data should be bytes" TypeError: POST data should be bytes or an iterable of bytes. It cannot be str."

What am I doing wrong ? I am using python 3.2.2

Below is the code:

msg = "Test post"
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
"http://twitter.com/statuses", "sampleusername", "password")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( {'status':msg} )
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()
2
  • I was lukcy all I had to do was input params = params.encode('utf-8') now I have a new problem urllib.error.HTTPError: HTTP Error 401: Unauthorized I am sure my username and password is right so what might be going wrong ? Commented Oct 4, 2011 at 14:08
  • Open another question for the authentication issue, and possibly mark Daniel's answer to close this question. Commented Oct 22, 2011 at 17:08

1 Answer 1

3

It means what it says - in Python 3, strings are unicode by default, but you can't post unicode: you have to use a bytestring.

This should work:

msg = b"Test post"
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.