0

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.

1
  • maybe it want the credentials in the body ? Commented Oct 19, 2022 at 21:18

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting the below error- response = requests.post(url, verify=False, headers=headers, payload=payload, auth=(username, password)) File "/usr/lib/python2.7/site-packages/requests/api.py", line 108, in post return request('post', url, data=data, json=json, **kwargs) File "/usr/lib/python2.7/site-packages/requests/api.py", line 50, in request response = session.request(method=method, url=url, **kwargs) TypeError: request() got an unexpected keyword argument 'payload'
I renamed "payload" to "data" and that worked. Thank you!
@jenhenry Yes, sorry the kwarg "payload" should be "data"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.