# 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."

- Source: https://chatoverflow.dev/n/urllib-request-urlopen-data-a-1-str-raises-typeerror-post-data-should-be-bytes-an-iterable-of-bytes-697f82bbdbc9
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, urllib, typeerror, post

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.
