I'm creating a Phyton script to POST a request which contains JSON BODY, This is the JSON input I need to pass in the body of the request :
{
"Name": "PServiceGroup125",
"ServiceIDs": [1330,2387]
}
I'm able to do this(it works) :
body = "{\"Name\": \"PServiceGroup125\",\"ServiceIDs\": [1330,2387]}"
headers = [ NVPair('Content-Type', 'application/json'), NVPair('UAMToken', uamtok) ]
response = self.httpPost(self.hostPort, self.URI, headers, body)
What I'm trying to achieve: ( to make it more "OO")
To create a function(say createBody ) to create a python object and then convert this python object into JSON and pass it in the request:
def createBody(self):
self.serviceDataProvider.setName("PTestServiceGroup %s" % time.strftime("%d%m%H%M%S"))
self.serviceDataProvider.setServiceIDs(self.serviceDataProvider.nextItem())
I have two questions here :
Here I set the "value" for the corresponding json name and service ID field, how should I set the name field(ServiceIDs)? ( "ServiceIDs": [1330,2387] )
Assuming that the "serviceDataProvider" object has been created successfully(with Name and ServiceID values ) .
how should i convert this object(serviceDataProvider) back to JSON and pass it in the request ?
I'm looking at the 'json' module , but not very clear on how to use it for this scenario
Anyones help and suggestion is appreciated !
# serviceDataProvider is object of class ServiceDataProvider
class ServiceDataProvider(URLDataProvider):
name =""
sid=''
sIDs=[]
def setName(self,name):
self.name=name
def getName(self):
return self.name
def setServiceIDs(self,sid):
sIDs=[]
sIDs.append(sid)
def getServiceIDs(self):
return self.sIDs