Get Current and Historic Point Values Through REST API
-
I'm trying to use the REST API to bring data from Mango into another system. I can authenticate and get data point information, however, I can't figure out how to get current values or historic values. Seems like I must be missing something simple in the documentation?
import requests from requests.auth import HTTPBasicAuth userName = 'myusername' token = 'mypassword' theAuth = HTTPBasicAuth(userName, token) session = requests.Session() response = session.get('https://monitoring.OurDomain.com/rest/v1/users/current', auth=theAuth) if response.status_code == 200: print("Success Logging In") payLoad = {'xid': 'internal_mango_uptime_hrs'} response = session.get('https://monitoring.OurDomain.com/rest/v1/data-points/', data=payLoad, auth=theAuth) print(response.status_code) print(response.text) else: print("Error") print(response.status_code) print(response.text) # log a auth fault
-
For the latest (current values) use this and set the limit to 1 for the most up to date value:
https://monitoring.[domain].com/rest/v2/point-values/{XID}
For historic you'll want:
//This one is a GET, use params https://monitoring.[domain].com/rest/v2/point-values/multiple-arrays/time-period/{xids}
Read in the swagger UI and you'll see the params you need to pass in the URL for both of these calls.
make sure you have access to the swagger API info by ensuring it's enabled in the env.properties file.
Fox
-
This post is deleted! -
Thanks @MattFox for the quick response. swagger is enabled and mango has been reset but now I'm getting a 403 error. I have confirmed the username and password are accurate. Any ideas?
payLoad = {'username': userName, 'password': password} session = requests.Session() response = session.post('https://monitoring.[domain].com/rest/v2/login', json=payLoad) if response.status_code == 200: print("Success Logging In") print(response.status_code) print(response.text) else: print("Error") print(response.status_code)
-
This thread may have useful insights: https://forum.infiniteautomation.com/topic/2658/any-generic-guide-to-rest-api-python/9
It could be a header issue, or it could be related to the XSRF token.
-
This fixed it and i can now login. thanks @MattFox and @phildunlap
payLoad = {'username': userName, 'password': password} session = requests.Session() session.headers.update({'Cookie':'XSRF-TOKEN=dc1d54de-fab1-4405-8ccb-ea677aadf7ca','X-Xsrf-Token':'dc1d54de-fab1-4405-8ccb-ea677aadf7ca','Accept': 'application/json', 'Accept-Encoding': 'gzip,deflate,sdch'}) # session.headers["Content-Type"] = "application/json" response = session.post('https://monitoring.[domain].com/rest/v2/login', json=payLoad)
-
Thanks Phil, haven't quite mastered the art of answering forum posts while catching 40 winks!
Good to see you're sorted Chio.Fox
-
I'm largely a librarian of previously answered questions at this point :D