crossbario / crossbar

Crossbar.io - WAMP application router
https://crossbar.io/
Other
2.05k stars 274 forks source link

how to send a small base64 jpg image via wamp from python to js front end #2039

Closed antonymott closed 1 year ago

antonymott commented 1 year ago

crossbar v21.3.1 linux

I realize wamp is generally for small messages, and the image I'm sending is tiny. But base64 from python is changed in transit, not a valid URL. In js, I also tried btoa() and atob() more out of desperation as it seems neither should be necessary as base64 is such a common network protocol.

pseudocode in python: @register('com.test.b64image) @inlineCallbacks def returnBase64(self): with open(uri, 'rb') as imageFile: encodedString = base64.b64encode(imageFile.read()) print(encodedString)

'/9j/4AAQSkZJRgABAQAAAQABAAD/4QDeRXhpZgAASUkqAAgAAAAGABIBAwABAAAAAQAAABoBBQABAAA... returnValue(encodedString)

in js: const res = await session'call' console.log(res) ...FCQUFELzRRRGVSWGhwWmdBQVNVa3FBQWdBQUFBR0FCSUJBd0FCQUFBQUFRQUFBQm9CQlF

The JavaScript string is completely changed.

oberstet commented 1 year ago

b64encode returns a binary, not a string. you need to add a decode() if you want it to be transported as a string

>>> import os, base64
>>> base64.b64encode(os.urandom(16))
b'3w0O75uLD2lkGye2QWlS2w=='
>>> base64.b64encode(os.urandom(16)).decode()
'IbNb3LSimKfUPPULMKZL+A=='
>>> 
antonymott commented 1 year ago

Problem solved...thanks Tobias!