In this example, we will perform an HTTP GET REST API request via Python:
- Open a text editor, input the following code and save it into the .py extension, for example NSX-PythonGET.py:
# Import Requests library import requests # NSX Variables nsxmanager = 'https://nsxmgr-01a.corp.local' nsxurl = '/api/2.0/services/usermgmt/user/admin' nsxheaders = {'Content-Type': 'application/xml'} nsxuser = 'admin' nsxpass = 'VMware1!' # REST API call using requests.get function from request library. Set verify to False to ignore SSL response = requests.get(nsxmanager + nsxurl, auth = (nsxuser, nsxpass), verify = False, headers = nsxheaders) # Print HTTP Response Code print (response) # Print XML Content print (response.text)
- Run the .py Python script from the command prompt:
You can also copy each line of code and paste it into the Python interpreter to see what is happening in the background. To use the Python interpreter, type python from the command prompt: