How do I use basic HTTP authentication with the Python Requests library?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
HTTP Basic authentication is simple: the client sends a username and password with each request. In Python, the requests library already knows how to build the correct Authorization header, so the main job is using that support cleanly and keeping credentials out of source code.
The simplest requests pattern
For one-off calls, pass a (username, password) tuple to the auth argument. requests converts it into the proper Basic auth header for you.
This is the most common answer because it is short and correct. It is also better than building the header manually, since the library handles encoding and request preparation consistently.
Use HTTPBasicAuth when you want explicit intent
The tuple form is convenient, but HTTPBasicAuth can make larger codebases easier to read.
This becomes useful when your application supports multiple authentication schemes and you want the chosen strategy to be obvious from the code.
Reuse credentials with a session
If you make several requests to the same service, create a Session. That gives you connection reuse and centralizes the auth configuration.
This pattern is cleaner than repeating auth= on every call, especially if you later add common headers, retry logic, or timeouts through a wrapper client.
Keep credentials out of code
Hard-coding secrets is fine for a quick demo and poor for real systems. Read them from the environment instead.
In production, those values might come from a secrets manager, container environment, or CI variables. The key point is that the application code should not be the secret store.
Handle failures separately from transport errors
A failed login and a broken network are different problems. A 401 Unauthorized response means the server answered the request and rejected the credentials. Connection timeouts or TLS issues raise exceptions instead.
Keeping those paths separate makes retry logic and error reporting much more accurate.
Manual headers are possible but rarely necessary
If you are debugging or writing a thin wrapper around another HTTP layer, you can build the header yourself. Most of the time, that is extra work without a benefit.
This works, but using auth= is safer and clearer unless you have a concrete reason not to.
Common Pitfalls
The biggest mistake is using Basic auth over plain HTTP. Base64 is not encryption, so anyone who can observe the traffic can recover the credentials. Use HTTPS.
Another problem is logging full request headers during debugging. That can leak the Authorization header into local logs, CI output, or monitoring systems. Redact it whenever request logging is enabled.
Developers also tend to repeat auth= everywhere instead of creating a session or dedicated API client. That increases duplication and makes later changes harder.
Finally, do not treat every failure as an authentication failure. A 401 is different from a DNS error, a socket timeout, or a certificate problem, and the fix depends on which one happened.
Summary
- Use
requests.get(..., auth=(user, password))for the simplest correct Basic auth call. - '
HTTPBasicAuthmakes the authentication strategy more explicit.' - '
requests.Session()is better for repeated authenticated requests.' - Store credentials outside the source code, such as in environment variables or a secret store.
- Use HTTPS and handle
401responses separately from transport exceptions.
Related reading
- How do I use minikube's DNS?
- How do I use WebRequest to access an SSL encrypted site using HTTPS?
- How do raft nodes learn about peers?
- How do SO_REUSEADDR and SO_REUSEPORT differ?
- How do I use the Argon2 algorithm with password_hash?
- How do I verify that an Android apk is signed with a release certificate?
- How do I use brew installed Python as the default Python?
- How do I use installed packages in PyCharm?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.