Vishal Khialani
Vishal Khialani

Reputation: 2587

Python error: TypeError: POST data should be bytes

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()

Upvotes: 2

Views: 3059

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599778

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"

Upvotes: 3

Related Questions