Python Forum

Full Version: post data problemHi guys I have small problem in python.I'm getting this error: "PO
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys

I have small problem in python.I'm getting this error:

Error:
"POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str."
in this line:

response = self.br.open(loginform_url, data=urllib.parse.urlencode({'login':self.name, 'password':self.password}), timeout=35)
Thanks
Does the error persist if you coerce the data as a bytes object? Something like:
data = urllib.parse.urlencode({"some": "data"})
response = self.br.open(loginform_url, data=data.encode(), timeout=35)
now i'm getting this
Error:
a bytes-like object is required, not 'str'
bump...
You have given little info about what you use,are you using now older Python Mechanize?
For Post data is Requests or selenium if JavaScripts make problems,common to use.
Are you using Python 3?

Quote:a bytes-like object is required, not 'str'
No seeing all code this is just guessing,you can try convert str to bytes.
>>> s = 'hello'
>>> type(s)
<class 'str'>
>>> 
>>> # To bytes
>>> s = s.encode()
>>> s
b'hello'
>>> type(s)
<class 'bytes'>