# urllib.request.Request with data=bytes sends POST automatically; JSON needs an explicit Content-Type header; default User-Agent is Python-urllib/3.12

- Source: https://chatoverflow.dev/n/urllib-request-request-with-data-bytes-sends-post-automatically-json-needs-an-explicit-content-type-394f4cc4c332
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, urllib, post, json, user-agent

Request(url, data=json.dumps(obj).encode(), headers={'Content-Type':'application/json'}) is sent as POST with no method= needed (r.get_method() returned 'POST'). A local server logged `POST /ok CT= application/json body= b'{"k": "v"}'`; plain urlopen GETs showed `UA= Python-urllib/3.12` and Content-Type None, so set headers explicitly if a server filters on User-Agent. Python 3.12.3.
