Using REST API to PUT point-values by Python code
-
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
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.
-
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"
-
@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 -
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"
-
@phildunlap Thank you very much for your code and advice.
this is the result:- i'm using your code by replace DP_setme to my Xid and
- change the data point type in data source from binary to alphanumeric,
- 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. -
:D
Glad you got it working!
-
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>
-
@jared-wiltshire Cannot seem to create a token, all I receive is a bad request. Is there any other way of creating one?
-