There are many string to bytes conversion question about Python 3 on Stackoverflow, each one treating slightly different cases, and as I couldn't find this specific one, I'll answer my own question here.
Some fields of a webservice, e.g. those that transport files like a PDF document, may do that base64 encoded.
It Python 2 this did work:
with open(filepath, 'w') as file_:
file_.write(my_content.decode('base64'))
Now, in Suds on Python 3 the equivalent would be:
from base64 import b64decode
file_.write(b64decode(my_content))
But this results in an error: a bytes-like object is required, not 'Text'
.
The reason is that Suds returns a custom type Text
which for b64encode
unexpectedly doesn't react like str
(altough it's subclassing it). So it must be converted explicitly to str
first:
from base64 import b64decode
file_.write(b64decode(str(my_content)))