I'm trying to post data to an Ignition tag using the WebDev module's doPost
method. Below is my setup:
-->WebDev doPost
Script (Module: Test
)
import os
import xml.etree.ElementTree as ET
from xml.dom import minidom
reqdict = str(request)
xmlResponse = "<RESPOK/>"
dsCols = ["key", "val"]
dsRows = [ ]
ssRows = [ ]
data = request['data']
xmlMessage = str(request["headers"]["Content-Type"])
#Create tag path
tn = str(system.date.toMillis(system.date.now()))
baseTagPath = "[Sample_Tags]New_Tag/Outbox"
tagType = "UdtInstance"
typeId = "[Sample_Tags]_types_/Newtags"
tag = {
"name": tn,
"typeId": typeId,
"tagType": tagType,
}
system.tag.configure(baseTagPath, [tag], "a")
#Read and decode data
if "text/xml" in str(request["headers"]["Content-Type"]):
strData = str(request['data'])
string_output = strData.decode('utf-8') # Possible issue here?
xmlMessage = strData
#Write to tags
for i in request:
dsRows.append([i, str(request[i])])
for i in session:
ssRows.append([i, str(session[i])])
btp = baseTagPath + "/" + tn
system.tag.writeBlocking(
[btp + "/lastRequest", btp + "/lastReqTS", btp + "/lastReqData", btp + "/lastSessionData"],
[xmlMessage, system.date.now(), system.dataset.toDataSet(dsCols, dsRows), system.dataset.toDataSet(dsCols, ssRows)]
)
system.tag.writeBlocking(
["[Sample_Tags]New_Tag/lastRequest", "[Sample_Tags]New_Tag/lastReqTS", "[Sample_Tags]New_Tag/lastReqData", "[Sample_Tags]New_Tag/lastSessionData"],
[xmlMessage, system.date.now(), system.dataset.toDataSet(dsCols, dsRows), system.dataset.toDataSet(dsCols, ssRows)]
)
return {'response': unicode(xmlResponse), 'contentType': "application/xml"}
-->Payload Sent via Script Console:
ewmUrl = "https://localhost:8088/system/webdev/Test"
payload = [{"data": "this is a testcase"}]
res = system.net.httpPost(ewmUrl, postData=payload, username=username, password=password)
print res
--> Response Output:
-->problem:
When I check the tag value (lastRequest
or inside Outbox/...
), I get something like:
array['b',[32]]
It seems like the posted data
is being interpreted as a byte array instead of a string, even though I'm sending a plain string.
What I Need Help With:
- How can I correctly extract the string content from
request['data']
? - Is the
decode('utf-8')
failing silently or misused? - Should I post the payload differently to avoid byte format?
Need your guidance to solve this issue.
Thanks in advance.