TypeError: POST data should be bytes or an iterable of bytes. It cannot be str

You did basically correct in trying to convert the string into bytes, but you did it the wrong way. Python doesn’t have typecasting (so what you did was not typecasting).

The way to do it is to encode the text data into bytes data, which you do with the encode function:

binary_data = data.encode('encoding')

What ‘encoding’ should be depends. You should probably use ‘ascii’ here. If you have characters that aren’t ASCII, then you need to use another encoding, typically ‘utf8’, but then you also need to tell the receiving webserver that it is UTF-8. It might also not want UTF8, but then you have to ask it, and it’s getting complicated. 🙂

Leave a Comment