Home > Enterprise >  Pass user input to authorization header in post request using python
Pass user input to authorization header in post request using python

Time:10-21

I want to send user input (username and password) to the Header in post request. I have this so far-

import requests                                                                                                                                                              
import urllib3                                                                                                                                                                                                                                                                                                         
import json                                                                                                                                                                                                                                                                                                                            
import getpass                                                                                                                                                               
urllib3.disable_warnings()                                                                                                                             
                                                                                                                                            
url = "https://127.0.0.1"                                                                                                                               
username = raw_input("Enter the username: ")                                                                                                                                 
password = getpass.getpass()                                                                                                                                                 
payload = "relevance=(members of computer group whose (name = \"ABC\") of computers)"                                                                                                                                     
                                                                                                                                                                             
headers = {                                                                                                                                                                                                                                                                                 
    'username': username,                                                                                                                                                    
    'password': password,                                                                                                                                                    
     'Content-Type': 'application/xml',                                                                                                                                      
}                                                                                                                                                                            
response = requests.post(url, verify=False, headers=headers, payload=payload)                                                                                                  
print(response)  

                                                                                                                                                   

I'm getting a 401 error. But when I use Basic auth, it works-

headers = {
    'Authorization': 'Basic akjrgbierhgiehg',
    'Content-Type': 'application/xml',
}

Please advise.

CodePudding user response:

Username and password go in the auth keyword of requests. Remove them from headers but pass them in requests.post

headers = {                                                                                                                                                                                                                                                                                                                                                                                                                                   
     'Content-Type': 'application/xml',                                                                                                                                      
}
response = requests.post(url, verify=False, headers=headers, payload=payload, auth=(username, password))
  • Related