Requesting the HTTP GET REST API via Python

In this example, we will perform an HTTP GET REST API request via Python:

  1. 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) 
 
  1. 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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.23.123