venkatklr Posted October 2, 2017 Report Posted October 2, 2017 how to GET data from API to variable and match with value and then POST it. I want to do it in python script. Quote
Nytro Posted October 2, 2017 Report Posted October 2, 2017 http://www.geeksforgeeks.org/get-post-requests-using-python/ ? Quote
venkatklr Posted October 2, 2017 Author Report Posted October 2, 2017 Hi Administrator, thanks for the link. I am trying to GET data from vmware orchestrator API and if i try to export json, getting this error "'unicode' object has no attribute 'json'" any idea? Quote
u0m3 Posted October 2, 2017 Report Posted October 2, 2017 Could you give some examples of what your code is, what you are trying to parse so on and so forth? Not all of us know what the API looks like... And I for one did not pass the psychic exam... 1 Quote
Active Members MrGrj Posted October 2, 2017 Active Members Report Posted October 2, 2017 (edited) Hello, In Python, it's really easy to work with API calls using the requests module. To install it, just do: pip install requests Unfortunately, depending on your version, the API is largely SOAP based. The simplest way to consume VMWare's API is via one of the existing SDKs. Now, moving further and looking at a newer version of VMWare docs, we can see that there's an already developed module to interfere with it named pyvmomi. You can follow the instructions from the github to set everything up. If you follow carefully those steps you will find this https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/ which contains tons of examples. Now, literally speaking, if you specifically need to play a bit with requests, you can do the following: 1. Import the needed modules 2. Make GET requests 3. Process the data 4. POST processed data 1. Import the needed modules import requests 2. Make GET requests GET_URL = 'https://httpbin.org/get' POST_URL = 'https://httpbin.org/post' data = requests.get(GET_URL).json() # this will be a json data structure containing the data you need The output of the above is: {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.1'}, 'origin': '86.120.146.226', 'url': 'https://httpbin.org/get'} 3. Process the data Let's suppose you want to modify the origin value into something else: data['origin'] = 'something else' 4. POST processed data r = requests.post(POST_URL, data=data) print(r.text) Now, the output of the above will be: { "args": {}, "data": "", "files": {}, "form": { "headers": [ "Accept", "Accept-Encoding", "Connection", "Host", "User-Agent" ], "origin": "something else", "url": "https://httpbin.org/get" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "147", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.18.1" }, "json": null, "origin": "86.120.146.226", "url": "https://httpbin.org/post" } Now, regarding your unicode issue, the error was raised because the data is a unicode/str variable, which means that you first have to make it a JSON object (which is basically a dict). In my example, you can see that I already called .json() which makes sure the data is a JSON object. But, if the returned data is a string, you won't be able to do that. To do that, you can use the json module: import json data = json.loads(data) Edited October 2, 2017 by MrGrj 1 1 2 Quote
venkatklr Posted October 2, 2017 Author Report Posted October 2, 2017 (edited) here is my code. from xml.etree.ElementTree import Element, SubElement, Comment,XML from schedularConf import EXECUTION_CONST (Execution_Const is in different python script and here is the code. import json import xmltodict import requests from xml.etree import ElementTree from string import Template class EXECUTION_CONST(): # Header for HTTP request. headers = {'Content-Type': 'application/xml'} # Availability Tests SCHEDULAR_LISTS = { " Availability Test": "https://servername/vco/api/workflows/workflowID" }) class schedule: def getData(self): try: list = EXECUTION_CONST.SCHEDULAR_LISTS; for name, url in list.iteritems(): mainUrl = url.split("workflows")[0]+"/tasks"; nameSch = name.split("-")[1]; # print (nameSch) list = requests.get(mainUrl,auth = ('username', 'password'),verify=False).text print (list) lists = json.dumps(list) #print (lists) # for l in lists: # if l == " Availability Test": # print(" Availability Test is available" ) # else: # print ("Availability Test is not available") # XML = '<task xmlns="http://www.vmware.com/vco"><name>'+nameSch+'</name><recurrence-cycle>one-time</recurrence-cycle><recurrence-start-date>2017-09-30T11:52:00+03:00</recurrence-start-date><workflow href="'+url+'"></workflow><start-mode>normal</start-mode></task>' # # print (XML) # res = requests.post(mainUrl,data=XML,headers=EXECUTION_CONST.headers, # auth = ('username', 'password'),verify=False).text # print (res); except Exception, e: print e return ; Edited October 3, 2017 by venkatklr Quote
venkatklr Posted October 2, 2017 Author Report Posted October 2, 2017 3 hours ago, MrGrj said: Hello, In Python, it's really easy to work with API calls using the requests module. To install it, just do: pip install requests Unfortunately, depending on your version, the API is largely SOAP based. The simplest way to consume VMWare's API is via one of the existing SDKs. Now, moving further and looking at a newer version of VMWare docs, we can see that there's an already developed module to interfere with it named pyvmomi. You can follow the instructions from the github to set everything up. If you follow carefully those steps you will find this https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/ which contains tons of examples. Now, literally speaking, if you specifically need to play a bit with requests, you can do the following: 1. Import the needed modules 2. Make GET requests 3. Process the data 4. POST processed data 1. Import the needed modules import requests 2. Make GET requests GET_URL = 'https://httpbin.org/get' POST_URL = 'https://httpbin.org/post' data = requests.get(GET_URL).json() # this will be a json data structure containing the data you need The output of the above is: {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.1'}, 'origin': '86.120.146.226', 'url': 'https://httpbin.org/get'} 3. Process the data Let's suppose you want to modify the origin value into something else: data['origin'] = 'something else' 4. POST processed data r = requests.post(POST_URL, data=data) print(r.text) Now, the output of the above will be: { "args": {}, "data": "", "files": {}, "form": { "headers": [ "Accept", "Accept-Encoding", "Connection", "Host", "User-Agent" ], "origin": "something else", "url": "https://httpbin.org/get" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "147", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.18.1" }, "json": null, "origin": "86.120.146.226", "url": "https://httpbin.org/post" } Now, regarding your unicode issue, the error was raised because the data is a unicode/str variable, which means that you first have to make it a JSON object (which is basically a dict). In my example, you can see that I already called .json() which makes sure the data is a JSON object. But, if the returned data is a string, you won't be able to do that. To do that, you can use the json module: import json data = json.loads(data) MrGrj, Thank you for providing article. it is very informative. I am getting dic type output after adding .json() to GET request. my requirement is first check for (Availability Test) for multiple urls and then post to vro. please refer XML (I am using that to create task in vro) here nameSch is for the name create like Availability and href="'+url+ is the server url . Please let me know how can do it. Quote