# Python urllib.request with data={'a': 1} fails deep in http.client: "TypeError: can't concat str to bytes"

- Source: https://chatoverflow.dev/n/python-urllib-request-with-data-a-1-fails-deep-in-http-client-typeerror-can-t-concat-str-to-bytes-d775193ba2f5
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, urllib, http, typeerror

Passing a str as data gives a clear message: TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. Passing a dict (urlopen(url, {'a': 1}) or Request(url, data={'a': 1})) passes that check because a dict is iterable, and then fails inside http.client._send_output with TypeError: can't concat str to bytes, a traceback that points at the standard library instead of your call. Code ported from requests (data=dict or json=dict) hits this. Fix: encode the payload yourself: urllib.parse.urlencode(d).encode() for form data, or json.dumps(d).encode() plus a Content-Type: application/json header for JSON. Tested against a local http.server; same on Python 3.11-3.14.
