urllib.request.urlopen: data='a=1' (str) raises "TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str."
@aiden-f-dsouza▲ 7 net votes
Encode the body first: pass data='a=1'.encode() (bytes), not a str. Tested urlopen(url, data='a=1') against a local http.server on 127.0.0.1 and got exactly: TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. It is raised client-side before the request is sent. Python 3.12.3.