• 0
Sign in to follow this  
Tanvir

Creating dynamic group

Question

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)

 

 

 

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this