Contentdata in python3 with python-sdk


(Tutj) #1

Hi,

I am using pythonSDK to develop a microservice with python3. However, it is not clear of declaring the contentdata of IOMessage. What i did is this:

    msg = IoMessage()
    msg.infotype = 'application/json'
    msg.infoformat = 'text/utf-8'
    msg.contentdata = bytearray(contentdata.encode())

But it raises the error: Error: ‘bytearray’ object has no attribute ‘encode’


(Serge Radinovich) #2

Which iofog python package are you using?

Just want to make sure you are using https://pypi.org/project/iofog/ (latest is v3.0.0-beta1)


(Tutj) #4

Hi,
I tried both of v3.0.0-beta1 and also 0.0.10. And when i install iofog from python3, it is 0.0.10 by default


(Serge Radinovich) #5

From the error it appears that your contentdata is a bytearray so all you need to do is call contentdata.decode() to set msg.contentdata as a string.


(Tutj) #6

Here is my snippet:

data = {
“time”: 1540855847710,
“speed”: 41.71445712,
“acceleration”: “0.52431”,
“rpm”: “2078.3”
}
contentdata = json.dumps(data)
try:
msg = IoMessage()
msg.infotype = ‘application/json’
msg.infoformat = ‘text/utf-8’
msg.contentdata = contentdata
except IoFogException as e:
print("Error: ", e)
IoClient.post_message_via_socket(msg)

Still, it raises the same error.


(Serge Radinovich) #7

v3.0.0-beta2 released to address this https://pypi.org/project/iofog/3.0.0b2/

Let me know if you have further issues. The Python SDK project is still immature and needs work. Thanks