<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[First attempt at Python Rest Get and Post to Mango]]></title><description><![CDATA[<p dir="auto">[0_1501750054383_Mango_Rest_V1.py](Uploading 100%)</p>
<p dir="auto">Hi,<br />
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.</p>
<p dir="auto">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</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">Any advise is highly appreciated!</p>
<p dir="auto">Rob</p>
<pre><code>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)
</code></pre>
]]></description><link>https://forum.mango-os.com/topic/2887/first-attempt-at-python-rest-get-and-post-to-mango</link><generator>RSS for Node</generator><lastBuildDate>Mon, 15 Jun 2026 17:28:18 GMT</lastBuildDate><atom:link href="https://forum.mango-os.com/topic/2887.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 03 Aug 2017 08:59:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to First attempt at Python Rest Get and Post to Mango on Thu, 03 Aug 2017 20:04:16 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
]]></description><link>https://forum.mango-os.com/post/15156</link><guid isPermaLink="true">https://forum.mango-os.com/post/15156</guid><dc:creator><![CDATA[phildunlap]]></dc:creator><pubDate>Thu, 03 Aug 2017 20:04:16 GMT</pubDate></item><item><title><![CDATA[Reply to First attempt at Python Rest Get and Post to Mango on Thu, 03 Aug 2017 16:53:41 GMT]]></title><description><![CDATA[<p dir="auto">Hi Rob,</p>
<p dir="auto">I'm wondering if you need to also be updating the X-Xsrf-Token header in your Rest_cookie_up_date method.</p>
]]></description><link>https://forum.mango-os.com/post/15153</link><guid isPermaLink="true">https://forum.mango-os.com/post/15153</guid><dc:creator><![CDATA[phildunlap]]></dc:creator><pubDate>Thu, 03 Aug 2017 16:53:41 GMT</pubDate></item></channel></rss>