How to use HTTP Basic Authentication requests using Python?

To make HTTP requests with Basic Authentication in python, we need to use requests and HTTPBasicAuth libraries.

1. Download and install requests library

$ pip install requests

2. Creating a Post request with BasicAuth

# importing the requests library 
import requests
from requests.auth import HTTPBasicAuth
# create new postRequest function
def postRequest(requests):  
    endpoint = "http://<your-domain>/api/<post-method>"  
    data= {'test':'Hello World'}
    r = = endpoint, data= data, auth=HTTPBasicAuth('Username', 'Password'))
    data = r.text 
    print("Get Reponse:%s"%data) 
# call the postRequest fucntion


Get Reponse:{"statusCode":200}

That’s it. I hope this will help. ?

Leave a Reply

Your email address will not be published. Required fields are marked *