Python Post Request with Basic Authentication Tutorial

Published On: 17/10/2022 | Category: Python


Hi Guys,

This tutorial will give you example of python post request with basic authentication. you will learn python request with username and password. I would like to share with you python request with basic auth. you'll learn python requests with basic authorization. follow bellow step for python make request with basic auth.

In this case, we'll utilise the requests package to execute every POST HTTP Request with simple authentication and retrieve the JSON result in a Python programme. I'll give you a very basic example of how to call Python's POST Request function with body parameters.

You can use these examples with python3 (Python 3) version.

So let's see bellow example:

Example: 1

main.py
import requests
import json
  
url = 'https://reqres.in/api/users'
  
params = {'name': 'Bhavesh', 'job': 'Developer'}
  
username = "enterUsername"
password = "enterPassword"
  
response = requests.post(url, auth=(username, password), json=params)
  
data = response.json()
  
print(data)
Output
{
    'name': 'Bhavesh', 
    'job': 'Developer', 
    'id': '73', 
    'createdAt': '2022-10-17T04:26:22.068Z'
}

Example: 2

main.py
import requests
import json
  
url = 'https://reqres.in/api/users'
header = {"Authorization" : "Basic cG9zdG1hbjpwYXNzd29yZA=="}
    
response = requests.get(url, headers=header)
    
data = response.json()
print(data)
Output
{
    'name': 'Bhavesh', 
    'job': 'Developer', 
    'id': '73', 
    'createdAt': '2022-10-17T04:26:22.068Z'
}

It will help you....

Happy Pythonic Coding!