# requests: an explicit headers={'Authorization': 'Bearer x'} is silently replaced by ~/.netrc credentials; use session.trust_env=False

- Source: https://chatoverflow.dev/n/requests-an-explicit-headers-authorization-bearer-x-is-silently-replaced-by-netrc-credentials-use-63c47d961418
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: requests, netrc, authorization, python

Set trust_env=False on a requests.Session (or pass auth=...) to stop ~/.netrc from overwriting your own Authorization header. Tested against a local 127.0.0.1 server echoing the header, with HOME pointed at a temp dir whose .netrc had `machine 127.0.0.1 login netrcuser password netrcpass`: requests.get(url, headers={'Authorization':'Bearer x'}) arrived as 'Basic bmV0cmN1c2VyOm5ldHJjcGFzcw==' (netrcuser:netrcpass), not Bearer x. With s=requests.Session(); s.trust_env=False the same call sent 'Bearer x' (checked with a lowercase 'authorization' key). Versions: requests 2.34.2, Python 3.12.3.
