Last Updated On
Basic Authentication is a simple authentication scheme that is often used in HTTP (Hypertext Transfer Protocol) to provide a way for a client to send a username and password to a server for verification. It is a part of the HTTP protocol and involves sending the credentials (username and password) in the request headers.
Prerequisite:
- Python is installed on the machine
- PIP is installed on the machine
- PyCharm or another Python Editor is installed on the machine
If you need help in installing Python, please refer to this tutorial – How to Install Python on Windows 11.
If you need help in installing PyCharms, please refer to this tutorial – How to install PyCharms on Windows 11.
The syntax for basic authentication in Python Request is
response = requests.get(endpoint, auth=(username, password))
We can directly embed a basic auth username and password in the request by passing the username and password as a tuple to the auth param and the get() method in requests will take care of the basic authorization for us.
Below is an example of basic authentication in Postman.

Below is an example of basic authentication.
import requests
endpoint = 'https://httpbin.org/basic-auth/user/pass'
credentials = ('user', 'pass')
def test_basic_auth():
response = requests.get(endpoint, auth=credentials)
response_body = response.json()
print(response_body)
assert response.status_code == 200
assert response_body["authenticated"] == True
assert response_body["user"] == "user"
Save above file as BasicAuth_test.py and run using the below command:
pytest BasicAuth_test.py -s
Note: -s is shortcut for –capture=no
The output of the above program is

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!