We consume a web service for various pieces of data including attachments. We would like to incorporate this functionality into ignition, so users could view documents within the app.
I go out, get the stream of data and save that binary as a word doc. This works fine in python (eclipse, idle, etc) obviously slightly different due to the ignition methods. However when we try and duplicate it in ignition we are unable to get the word doc to build correct, as it is always corrupted when we try and open it. I’m guessing this is some kind of encoding issue?
Does anyone have any ideas what I could be doing wrong?
from __future__ import with_statement
authUrl="https://examplehost.net/rest/auth/1/login"
getUrl="https://examplehost.net/api/v2/workspaces/53/files/142"
password='MYPASSWORD'
headers={"Content-Type" : "application/json", "Accept": "application/json", "userID" : "ME", "password" :password}
sesh=system.util.jsonDecode(system.net.httpPost(authUrl , headers))
#get authorization
token, sessionId= sesh['customerToken'], sesh['sessionid']
#get data
headers={"Accept" : "application/octet-stream", "Cookie": str("customer=%s; JSESSIONID=%s" % (token, sessionId))}
obj=system.net.httpGet(getUrl, headerValues=headers)
#try and write
path=r'C:\Users\USERNAME\Desktop\file.doc'
with open(path, 'wb') as f:
#this writes but when you try and open it, it fails as corrupt
f.write(obj)
You could also try using Python’s urllib2 library. Here is a tutorial on that which includes how to set the headers for a request: pythonforbeginners.com/pytho … in-python/
I can actually consume the service just fine, in both environments (I’m actually using urllib2 in eclipse) I return a string (what appears to be a identical string), it’s just for some reason when I create the document from ignition it’s corrupted.
This is why I think it’s some sort of encoding issue, but I’ve tried a bunch of different formats, none seem to work.
I did try urllib2 initially (code below), but I get this error which drove me to the inductive methods.
[quote]urllib2.URLError: <urlopen error (-1, 'SSL exception: Differences between the SSL socket behaviour of cpython vs. jython are explained on the wiki: wiki.python.org/jython/NewSocket … L_Support’)>
[/quote]
I couldn’t get it to work (even after following the wiki instructions).
I then found a thread where Colby posted an SSL work around, but it failed in the wrapper function.