FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important
information on the platform change.
04-22-2024 04:18 PM
Hi
I am trying to add a variable to the datalayer using the node red example as a staring point. However, I would like to implement the request in Python instead.
import requests
class getData:
def __init__(self):
self.data = []
Auth_URL = "https://localhost/identity-manager/api/v2/auth/token"
ServerVar_URL = "https://localhost/automation/api/v2/nodes/" + "samples/kvd/implicit/xyz/abcd"
def makeVarables(opcVar: dict, user: str, pwd: str):
# Make the datalayer entries. Key Value database app must be installed!
# Authorisation
session = requests.Session()
session.verify = False
print(getData.ServerVar_URL)
response = session.post(getData.Auth_URL, json={"name": user, "password": pwd}).json()
token = response['access_token']
data_token = {"Authorization": f"Bearer {token}"}
payload = "{'type':'int32','value': 5}"
r = session.put(url=getData.ServerVar_URL, data={'type':'int32','value':0}, headers=data_token)
print('Status Code=%s'%(r.status_code))
print(r.json())
Unfortunately, I can not figure out the correct syntax of the data to be sent. I get the following response:
Status Code=400
{'type': 'about:blank', 'title': 'DL_TYPE_MISMATCH', 'status': 400, 'detail': "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid literal; last read: 'ty'", 'instance': 'samples/kvd/implicit/xyz/abcd', 'severity': 'Error'}
Thanks for your help
Reto
Solved! Go to Solution.
04-22-2024 06:50 PM
Hi @Reto ,
Modified your example slightly below. The data argument of requests.put() needs to be passed as a JSON string. I just used json.dumps() from the json library.
import requests
import json
class getData:
def __init__(self):
self.data = []
Auth_URL = "https://localhost/identity-manager/api/v2/auth/token"
ServerVar_URL = "https://localhost/automation/api/v2/nodes/" + "samples/kvd/implicit/xyz/g"
def makeVarables(user: str, pwd: str):
# Make the datalayer entries. Key Value database app must be installed!
# Authorisation
session = requests.Session()
session.verify = False
print(getData.ServerVar_URL)
response = session.post(getData.Auth_URL, json={"name": user, "password": pwd}).json()
token = response['access_token']
data_token = {"Authorization": f"Bearer {token}"}
payload = {
"type": "int32",
"value": 315
}
r = session.put(url=getData.ServerVar_URL, data=json.dumps(payload), headers=data_token)
print('Status Code=%s' % (r.status_code))
print(r.json())
test = getData
test.makeVarables('boschrexroth', 'boschrexroth')
exit(-1)