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 = requests.post(url = endpoint, data= data, auth=HTTPBasicAuth('Username', 'Password')) data = r.text print("Get Reponse:%s"%data) # call the postRequest fucntion postRequest(requests)
Output
Get Reponse:{"statusCode":200}
Thatβs it. I hope this will help. ?
Photo: https://www.brandeps.com