I’m running into a 401 error when trying to post data. However, the same data entered in a curl request works just fine. I have tried multiple attempts at trying get a 201 or 200 response.
<code>import requests
import arubaapi
import json
import getpass
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings()
dummyUsernam = input("Please enter your username: ")
print("n")
dummyPassword = getpass.getpass("please enter your password: ")
print("n")
aosDeviceFqdn = 'sjc04-wwi-mm-node1.mine.com'
def aosLogin():
#dummyUsernam = input("Please enter your username: ")
#print("n")
#dummyPassword = getpass.getpass("please enter your password: ")
#print("n")
"""Logs into the AOS device, returns <class requests.Session()> object and UID string"""
session = requests.Session()
myUrl = f'https://{aosDeviceFqdn}:4343/v1/api/login'
credentials = {
'username': dummyUsernam,
'password': dummyPassword,
}
#try:
#loginResponse = session.get( url=myUrl, params=credentials, verify=False )
loginResponse = session.post( url=myUrl, data=credentials, verify=False ) # same result
#except requests.exceptions.ConnectionError:
#print(f"Failed to reach {aosDeviceFqdn}, check that you are connected to VPN and that {aosDeviceFqdn} resolves properly in DNS")
#exit()
#except KeyError:
#print ("Please check the username and password, and that your account has permission to log in and is not locked.")
#exit()
jsonLoginResponse = loginResponse.json()
arubaUid1 = jsonLoginResponse['_global_result'] # this method of retrieving the UID is only available in the response of the login action
#arubaUid2 = getUidFromSession(session) # this is the same result as arubaUid1, extracted a different way, available anytime an open, logged-in session exists
global sub
sub = arubaUid1.get("UIDARUBA")
print(sub)
return sub
def func_2():
myUrl1 = ('https://10.183.73.15:4343/v1/configuration/object/airmatch_ap_freeze?config_path=%2Fmd%2FTesla-Workplace%2FSJC51')
myHeaders1 = {'Content-Type': 'application/json','Accept': 'application/json'}
myParams1 = {'UIDARUBA': sub}
# shut
myData1 = r'{ "ap-name":"sjc51-api-5m-10" , "channel":"104-" , "band_info":"5GHz" }'
response = requests.post(url=myUrl1, headers=myHeaders1, params=myParams1, data=myData1, verify=False)
print(response)
#return sub
aosLogin()
func_2()
############End of Code#########
output:
Desktop % python3 start_over_api_call.py
Please enter your username: myusername
please enter your password:
NzI1MDBmMmItYjg0Mi00MGJjLTkwODctZGU0
<Response [401]>
</code>
<code>import requests
import arubaapi
import json
import getpass
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings()
dummyUsernam = input("Please enter your username: ")
print("n")
dummyPassword = getpass.getpass("please enter your password: ")
print("n")
aosDeviceFqdn = 'sjc04-wwi-mm-node1.mine.com'
def aosLogin():
#dummyUsernam = input("Please enter your username: ")
#print("n")
#dummyPassword = getpass.getpass("please enter your password: ")
#print("n")
"""Logs into the AOS device, returns <class requests.Session()> object and UID string"""
session = requests.Session()
myUrl = f'https://{aosDeviceFqdn}:4343/v1/api/login'
credentials = {
'username': dummyUsernam,
'password': dummyPassword,
}
#try:
#loginResponse = session.get( url=myUrl, params=credentials, verify=False )
loginResponse = session.post( url=myUrl, data=credentials, verify=False ) # same result
#except requests.exceptions.ConnectionError:
#print(f"Failed to reach {aosDeviceFqdn}, check that you are connected to VPN and that {aosDeviceFqdn} resolves properly in DNS")
#exit()
#except KeyError:
#print ("Please check the username and password, and that your account has permission to log in and is not locked.")
#exit()
jsonLoginResponse = loginResponse.json()
arubaUid1 = jsonLoginResponse['_global_result'] # this method of retrieving the UID is only available in the response of the login action
#arubaUid2 = getUidFromSession(session) # this is the same result as arubaUid1, extracted a different way, available anytime an open, logged-in session exists
global sub
sub = arubaUid1.get("UIDARUBA")
print(sub)
return sub
def func_2():
myUrl1 = ('https://10.183.73.15:4343/v1/configuration/object/airmatch_ap_freeze?config_path=%2Fmd%2FTesla-Workplace%2FSJC51')
myHeaders1 = {'Content-Type': 'application/json','Accept': 'application/json'}
myParams1 = {'UIDARUBA': sub}
# shut
myData1 = r'{ "ap-name":"sjc51-api-5m-10" , "channel":"104-" , "band_info":"5GHz" }'
response = requests.post(url=myUrl1, headers=myHeaders1, params=myParams1, data=myData1, verify=False)
print(response)
#return sub
aosLogin()
func_2()
############End of Code#########
output:
Desktop % python3 start_over_api_call.py
Please enter your username: myusername
please enter your password:
NzI1MDBmMmItYjg0Mi00MGJjLTkwODctZGU0
<Response [401]>
</code>
import requests
import arubaapi
import json
import getpass
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings()
dummyUsernam = input("Please enter your username: ")
print("n")
dummyPassword = getpass.getpass("please enter your password: ")
print("n")
aosDeviceFqdn = 'sjc04-wwi-mm-node1.mine.com'
def aosLogin():
#dummyUsernam = input("Please enter your username: ")
#print("n")
#dummyPassword = getpass.getpass("please enter your password: ")
#print("n")
"""Logs into the AOS device, returns <class requests.Session()> object and UID string"""
session = requests.Session()
myUrl = f'https://{aosDeviceFqdn}:4343/v1/api/login'
credentials = {
'username': dummyUsernam,
'password': dummyPassword,
}
#try:
#loginResponse = session.get( url=myUrl, params=credentials, verify=False )
loginResponse = session.post( url=myUrl, data=credentials, verify=False ) # same result
#except requests.exceptions.ConnectionError:
#print(f"Failed to reach {aosDeviceFqdn}, check that you are connected to VPN and that {aosDeviceFqdn} resolves properly in DNS")
#exit()
#except KeyError:
#print ("Please check the username and password, and that your account has permission to log in and is not locked.")
#exit()
jsonLoginResponse = loginResponse.json()
arubaUid1 = jsonLoginResponse['_global_result'] # this method of retrieving the UID is only available in the response of the login action
#arubaUid2 = getUidFromSession(session) # this is the same result as arubaUid1, extracted a different way, available anytime an open, logged-in session exists
global sub
sub = arubaUid1.get("UIDARUBA")
print(sub)
return sub
def func_2():
myUrl1 = ('https://10.183.73.15:4343/v1/configuration/object/airmatch_ap_freeze?config_path=%2Fmd%2FTesla-Workplace%2FSJC51')
myHeaders1 = {'Content-Type': 'application/json','Accept': 'application/json'}
myParams1 = {'UIDARUBA': sub}
# shut
myData1 = r'{ "ap-name":"sjc51-api-5m-10" , "channel":"104-" , "band_info":"5GHz" }'
response = requests.post(url=myUrl1, headers=myHeaders1, params=myParams1, data=myData1, verify=False)
print(response)
#return sub
aosLogin()
func_2()
############End of Code#########
output:
Desktop % python3 start_over_api_call.py
Please enter your username: myusername
please enter your password:
NzI1MDBmMmItYjg0Mi00MGJjLTkwODctZGU0
<Response [401]>
Can anyone help identify what the issue could be? Ideally my account and creds are authorized as it works in Curl. I feel like the session is closing and the token is being rendered as expired.