• Recent
    • Tags
    • Popular
    • Register
    • Login

    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 Mango 5 Documentation Website

    Using REST API to PUT point-values by Python code

    How-To
    4
    9
    2.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      thirawad
      last edited by

      Hi all,
      this is the first time for me to using REST API . i'm start learning from SWAGGER, everything in swagger page working good. i can get value or put value in to the data point via Xid, it easy to use and power full. in figure below show for detail of PUT point-values/{Xid} had success.

      after that i'm try to get and put value from point-values via Python code. The Cookie issue for request header are solve in help forum. i'm trying several time. so it working only for request get() value, but when using request put() i receive response of request status code is 403. maybe the code is not correct, i don't know how to declare the body content of payload. i wish someone help. i have the code in below

      0_1515669082562_rest2.png
      0_1515669115056_rest1.png

      import requests
      import json
      
      ## STEP [1]-------------------------------------------------------------------------
      s = requests.Session()
      s.headers.update({'Cookie':'MANGO8080=1mk606ffnwaxywwielclj4ck2; XSRF-TOKEN=dc1d54de-fab1-4405-8ccb-ea677aadf7ca','X-Xsrf-Token':'dc1d54de-fab1-4405-8ccb-ea677aadf7ca','Accept': 'application/json', 'Content-Length': '39', 'Accept-Encoding': 'gzip', 'Content-Type': 'application/json;charset=UTF-8'});
      
      url3 = 'http://localhost:8080/rest/v2/login'
      js = {'username': "admin", 'password': "admin"}
      r = s.post(url3, data=json.dumps(js));
      
      s.headers.update({'Cookie' : r.headers['Set-Cookie']})
      del s.headers["Content-Length"]
      del s.headers["Content-Type"]
      del s.headers["Cookie"]
      del s.headers["X-Xsrf-Token"]
      
      ## STEP [2]-- get device name -----------------------------------------------------------------------
      r = s.get('http://localhost:8080/rest/v1/device-names', timeout=5);
      print ('\n device name')
      print( r.text)
      
      ## STEP [3]-- get Point value by {Xid} ---------------------------------------------------------------
      r = s.get('http://localhost:8080/rest/v1/point-values/DP_6bcc108f-faa1-47fb-b310-1acc8b047fd2/latest?useRendered=false&unitConversion=false&limit=1&useCache=true', timeout=5);
      print ('\n Point value')
      print( r.text)
      
      ## STEP [4]------ put Point value by {Xid} ---------------------------------------------------------
      dd = {'dataType': "BINARY",  'value': "true",  'timestamp': 0,  'annotation': "rest by TST 12"}      
      url2 = 'http://localhost:8080/rest/v1/point-values/DP_6bcc108f-faa1-47fb-b310-1acc8b047fd2?unitConversion=false'
      r = s.put(url2, data=json.dumps(dd));
      print ('\n post')
      print(r)
      print( r.text)
      

      the request status code in step 1 - 3 is <200> but step 4 is <403> please help !

      Best regard.

      1 Reply Last reply Reply Quote 0
      • phildunlapP
        phildunlap
        last edited by

        Hi thirawad,

        You are very close!

        You need to set the X-Xsrf-Token header to the XSRF-TOKEN value in the Set-Cookie response, so instead of

        del s.headers["X-Xsrf-Token"]
        

        do

        s.headers["X-Xsrf-Token"] = s.cookies["XSRF-TOKEN"]
        

        and set the content type before that PUT request for setting the value,

        s.headers["Content-Type"] = "application/json"
        
        T 1 Reply Last reply Reply Quote 0
        • T
          thirawad @phildunlap
          last edited by

          @phildunlap , Hi phildunlap, Thank you for your reply.
          for now my request PUT still had code 403 (Forbidden). i found some content miss match in header about set-cookie, MANGO8080 and XSRF-TOKEN, there follow with Path=/;HttpOnly, , i'm not sure. please advice me for correcting the code.
          [my device: window10, 64bit , python2.7.14 and Mango REST API V3.2.2]

          #--- code  
          print r.headers['Set-Cookie']
          
          #-- output 
          MANGO8080=nq3nbgdw1qymm6btf374f6wb;Path=/;HttpOnly, XSRF-TOKEN=63164c0e-4353-40dc-9591-30d129580c87;Path=/
          
          import requests
          import json
          
          
          s = requests.Session()
          s.headers.update({'Cookie':'MANGO8080=1mk606ffnwaxywwielclj4ck2; XSRF-TOKEN=dc1d54de-fab1-4405-8ccb-ea677aadf7ca','X-Xsrf-Token':'dc1d54de-fab1-4405-8ccb-ea677aadf7ca','Accept': 'application/json', 'Content-Length': '39', 'Accept-Encoding': 'gzip', 'Content-Type': 'application/json;charset=UTF-8'});
          url3 = 'http://localhost:8080/rest/v2/login'
          js = {"username": "admin", "password": "admin"}
          r = s.post(url3, data=json.dumps(js));
          
          ##STEP [1]-----------------------------
          print "After post login the status code is:", r.status_code
          print r.headers['Set-Cookie']
          
          ###----- Renew Cookie Token generate in cookie content
          s.headers.update({'Cookie' : r.headers['Set-Cookie']})
          
          ##------ Renew X-Xsrf-Token
          s.headers["Content-Type"] = "application/json"
          s.headers["X-Xsrf-Token"] = s.cookies["XSRF-TOKEN"]
          
          dd = {"dataType":"BINARY","value":"false","timestamp": "","annotation":"Rest by admin"}
          url2 = 'http://localhost:8080/rest/v1/point-values/DP_6bcc108f-faa1-47fb-b310-1acc8b047fd2?unitConversion=false'
          r = s.put(url2, data=json.dumps(dd));
          
          ##STEP [2]-----------------------------
          print(r)
          print(s.headers)
          
          

          Best regard.
          Thirawad

          1 Reply Last reply Reply Quote 0
          • phildunlapP
            phildunlap
            last edited by phildunlap

            Hmm. I did test your script, here was what I had,

            import requests
            import json
            
            ## STEP [1]-------------------------------------------------------------------------
            s = requests.Session()
            s.headers.update({'Cookie':'MANGO8080=1mk606ffnwaxywwielclj4ck2; XSRF-TOKEN=dc1d54de-fab1-4405-8ccb-ea677aadf7ca','X-Xsrf-Token':'dc1d54de-fab1-4405-8ccb-ea677aadf7ca','Accept': 'application/json', 'Content-Length': '39', 'Accept-Encoding': 'gzip', 'Content-Type': 'application/json;charset=UTF-8'});
            
            url3 = 'http://localhost:8080/rest/v2/login'
            js = {'username': "admin", 'password': "admin"}
            r = s.post(url3, data=json.dumps(js));
            
            del s.headers["Content-Length"]
            del s.headers["Content-Type"]
            del s.headers["Cookie"]
            s.headers["X-Xsrf-Token"] = s.cookies["XSRF-TOKEN"]
            
            ## STEP [2]-- get device name -----------------------------------------------------------------------
            r = s.get('http://localhost:8080/rest/v1/device-names', timeout=5);
            print ('\n device name')
            print( r.text)
            
            ## STEP [3]-- get Point value by {Xid} ---------------------------------------------------------------
            r = s.get('http://localhost:8080/rest/v1/point-values/DP_setme/latest?useRendered=false&unitConversion=false&limit=1&useCache=true', timeout=5);
            print ('\n Point value')
            print( r.text)
            
            ## STEP [4]------ put Point value by {Xid} ---------------------------------------------------------
            s.headers["Content-Type"] = "application/json"
            dd = {'dataType': "ALPHANUMERIC",  'value': "true",  'timestamp': 0,  'annotation': "rest by TST 12"}      
            url2 = 'http://localhost:8080/rest/v1/point-values/DP_setme?unitConversion=false'
            r = s.put(url2, data=json.dumps(dd));
            print ('\n put')
            print(r)
            print( r.text)
            

            You may with to pass your "value" in the put as a boolean, like, "value": false instead of as a string, "value":"false"

            T 1 Reply Last reply Reply Quote 0
            • T
              thirawad @phildunlap
              last edited by

              @phildunlap Thank you very much for your code and advice.
              this is the result:

              1. i'm using your code by replace DP_setme to my Xid and
              2. change the data point type in data source from binary to alphanumeric,
              3. It done, i'm receive response code: <201>

              for the next i will try to change the way for put as a boolean.
              thank you again. you very please me.

              1 Reply Last reply Reply Quote 0
              • phildunlapP
                phildunlap
                last edited by

                :D

                Glad you got it working!

                1 Reply Last reply Reply Quote 0
                • Jared WiltshireJ
                  Jared Wiltshire
                  last edited by

                  For anyone reading this thread, if you use Mango v3.3 you can now generate JWT authentication tokens from the users page on the new UI and use these to authenticate instead of logging in from your script.

                  You do not need to use any CSRF/XSRF protection cookies/headers when using JWT authentication.

                  Just set the one header on every request -
                  Authorization: Bearer <token>

                  Developer at Radix IoT

                  MattFoxM 1 Reply Last reply Reply Quote 1
                  • MattFoxM
                    MattFox @Jared Wiltshire
                    last edited by

                    @jared-wiltshire Cannot seem to create a token, all I receive is a bad request. Is there any other way of creating one?

                    Do not follow where the path may lead; go instead where there is no path.
                    And leave a trail - Muriel Strode

                    1 Reply Last reply Reply Quote 0
                    • Jared WiltshireJ
                      Jared Wiltshire
                      last edited by

                      @MattFox See the post here -
                      https://forum.infiniteautomation.com/topic/3238/failed-to-create-authentication-token-bad-request/2

                      Developer at Radix IoT

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post