Forum Discussion

Tanvir's avatar
7 years ago

Creating dynamic group

Hi All

I am trying to create groups with a python script which read a CSV file. 

my txt file has following information

{"name":1","parentId":462,"appliesTo": "Type==\\"Test Server\\\"\"}
{"name":2","parentId":462,"appliesTo": "Type==\\"Test Server\\\"\"}
{"name":3","parentId":462,"appliesTo": "Type==\\"Test Server\\\"\"}
{"name":4","parentId":462,"appliesTo": "Type==\\"Test Server\\\"\"}
 

when I run the script bellow I receive Invalid JSON body message 

 

Response Status: 200
Response Body: b'{"data":null,"errmsg":"Invalid json body","status":1007}'

I can create the group if I assign value directly like data ='{"name":1","parentId":462,"appliesTo": "Type==\\"Test Server\\\"\"}'

Script is 

Quote
Quote


import requests
import json
import hashlib
import base64
import time
import hmac

#Account Info
AccessId ='tba'
AccessKey ='XXX'
Company = 'YYY'

#Request Info
httpVerb ='POST'
resourcePath = '/device/groups'

with open('test.txt', 'r') as file:
    #csv_reader = csv.reader(csv_file)
    for line in file:
        data =line    
        print (data)


        url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath 

        #Get current time in milliseconds
        epoch = str(int(time.time() * 1000))

        #Concatenate Request details
        requestVars = httpVerb + epoch + data + resourcePath

        #Construct signature

        hmac1 = hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
        signature = base64.b64encode(hmac1.encode())

        #Construct headers
        auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
        headers = {'Content-Type':'application/json','Authorization':auth}

        #Make request
        response = requests.post(url, data=data, headers=headers)

        #Print status and body of response
        print ('Response Status:',response.status_code)
        print ('Response Body:',response.content)