The requests library is built on top of the urllib3 library. So, when you pass None User-Agent header to the requests's post method, the urllib3 set their own default User-Agent
import requests
r = requests.post("https://httpbin.org/post", headers={
"User-Agent": None,
})
print(r.json()["headers"]["User-Agent"])
Output
python-urllib3/1.26.7
Here the urllib3 source of connection.py
class HTTPConnection(_HTTPConnection, object):
...
def request(self, method, url, body=None, headers=None):
if headers is None:
headers = {}
else:
# Avoid modifying the headers passed into .request()
headers = headers.copy()
if "user-agent" not in (six.ensure_str(k.lower()) for k in headers):
headers["User-Agent"] = _get_default_user_agent()
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
So, you can monkey patch it to disable default User-Agent header
import requests
from urllib3 import connection
def request(self, method, url, body=None, headers=None):
if headers is None:
headers = {}
else:
# Avoid modifying the headers passed into .request()
headers = headers.copy()
super(connection.HTTPConnection, self).request(method, url, body=body, headers=headers)
connection.HTTPConnection.request = request
r = requests.post("https://httpbin.org/post", headers={
"User-Agent": None,
})
print(r.json()["headers"])
Output
{
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Content-Length': '0',
'Host': 'httpbin.org',
'X-Amzn-Trace-Id': 'Root=1-61f7b53b-26c4c8f6498c86a24ff05940'
}
Also, consider to provide browser-like User-Agent like this Mozilla/5.0 (Macintosh; Intel Mac OS X 12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36. Maybe it solves your task with less effort