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

后端 未结 2 2093
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 05:56

I just updated from python 3.1 to python 3.2 (formatted HD) and one of my scripts stopped working. It gives me the error in the title.

I would fix it myself but I do

2条回答
  •  迷失自我
    2020-11-27 06:36

    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. :-)

提交回复
热议问题