Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

First attempt at Python Rest Get and Post to Mango


  • [0_1501750054383_Mango_Rest_V1.py](Uploading 100%)

    Hi,
    I'm new to Mango so my apologies for any 'school boy errors'. However I've spent the last couple of days writing a Python script to update a data point in Mango.

    I know that the point has been correctly enabled in Mango as I've been able to write to it from a remote machine via swagger :- URL//swagger/index.html#!/point-values/putPointValue

    I've also been able to get the users and sources data from Mango via my script. Please see attached. However were I'm falling down is the PUT. I notices that when I ran Wireshark and inspected the HTTP packet the formation of the cookie is slightly different between the putPointValue undate via Swagger and my Python script.

    I'm therefore uncertain if I've returning the cookie correctly to Mango (I started splitting out and reforming the cookie but this didn't work) or whether my JSON transfer is wrong. I'd assumed the JSON transfer was indentical in all cases.

    Any advise is highly appreciated!

    Rob

    import requests
    import json
    import time
    
    
    class Rest_login():
        def __init__(self):
            self.rest_obj = requests.Session()
            self.targetlogin = 'URL/rest/v2/login'
            self.target_allpoints = 'URL/rest/v1/data-points/explain-query'
            self.target_point = 'URL/rest/v1/data-points/The_Snake_Rock!/'
            self.target_allsources = 'URL/rest/v1/data-sources/explain-query'
            self.target_allusers = 'URL/rest/v1/users'
            self.target_alldevices = 'URL/rest/v1/device-names'
            self.rest_obj.headers.update({'Accept':'application/json','Content-Length':'39', 'Content-Type': 'application/json;charset=UTF-8','Cookie':'XSRF-TOKEN=74cf354a-e871-48b6-a1c2-bebb93d00120','X-XSRF-TOKEN':'74cf354a-e871-48b6-a1c2-bebb93d00120'});
            self.json = {'username': "admin", 'password':"admin"}
            self.test_val = {"dataType": "ALPHANUMERIC","value": "123456","timestamp": "","annotation": ""}
            self.snake_first_array = 'URL/rest/v1/point-values/DP_72f90749-025a-497f-9afb-f36698f14c35'
            self.login_obj = self.rest_obj.post(self.targetlogin, data = json.dumps(self.json))
            self.cookie = {}
            self.counter = 0
            #self.split_cookie_str_2 = ''
            
        def Rest_cookie_up_date(self):
            self.cookie = self.login_obj.headers['Set-Cookie']
            print self.cookie
            print len(self.cookie)
            #Break out the XSRF-Token
            self.split_cookie = self.cookie.split(",")
            self.split_cookie_str_1 = str(self.split_cookie[1])
            print self.split_cookie_str_1
            self.split_cookie_2 = self.split_cookie_str_1.split(";")
            self.split_cookie_str_2 = str(self.split_cookie_2[0]).lstrip(' ')
            print self.split_cookie_str_2
            #Break out the Mango 80
            self.split_cookie_3 = self.cookie.split(";")
            self.split_cookie_str_3 = str(self.split_cookie_3[0])
            print self.split_cookie_str_3
            self.new_cookie_str = self.split_cookie_str_2 + '; ' + self.split_cookie_str_3
            print self.new_cookie_str
            #self.rest_obj.headers.update({'Cookie':self.new_cookie_str})
            self.rest_obj.headers.update({'Cookie':self.cookie})
       
            
        def Rest_get(self):
            self.all_users_get = self.rest_obj.get(self.target_allusers, timeout = 5)
            print self.all_users_get
            print self.all_users_get.text
            self.all_points_get = self.rest_obj.get(self.target_allpoints, timeout = 5)
            print self.all_points_get.text
            self.all_sources_get = self.rest_obj.get(self.target_allsources, timeout = 5)
            print self.all_sources_get.text
            self.all_devices_get = self.rest_obj.get(self.target_alldevices, timeout = 5)
            print self.all_devices_get.text
            
        def Rest_post(self):
            self.rest_post_val = self.rest_obj.put(self.snake_first_array, data = json.dumps(self.test_val))
            print 'POST_VALUE_BELOW'
            print self.rest_post_val
            print 'POST_VALUE_ABOVE'
        
            
            
    if __name__ == "__main__":  
        Rest_obj = Rest_login()
        Update_obj = Rest_obj.Rest_cookie_up_date()
        while True:
           
            number_1 = Rest_obj.Rest_post()
            time.sleep (1)
            number_2 = Rest_obj.Rest_get()
            time.sleep(2)
    

  • Hi Rob,

    I'm wondering if you need to also be updating the X-Xsrf-Token header in your Rest_cookie_up_date method.


  • I just noticed you'll also need to remove your Content-Length header. I would have expected it to handle that for you in making the post() call.