Base64 text decoding

Hello,
I’m usig an Httprequest widget (nammed “test”) to retrieve data from an API. The API replies with a JSON object.

I use the following script to decode the JSON, select the field I need (payload) and send the result to the Text value of a text widget:

function () {
    return JSON.parse(document.childByName("test").content.httpText).payload
}

It works; but the value of the 'payload" field I receive from the API is base64 encoded.

So I tried to use this to decode it:

function () {
    return atob(JSON.parse(document.childByName("test").content.httpText).payload)
}

But it doesn’t work. I have the following error on the text widget:

ReferenceError: atob is not defined

How can I solve this please ?

Looking into the 2.3 update; it seems base64 conversion for text is already supported:

String: Added text conversion function in Base64 and Hex.

But how does it work ? I was unable to find documentation on this.

Thank you for your help
Best regards

text to base64 - yes
base64 to text - no
2023-10-19_09-11-11
base64.flp (6.1 KB)

It can be done using qml code editor:

import QtQuick 2.12

Item {
    Connections {
        target: document.childByName("DecodeButton").scriptAdaptor
        onEventItemClicked: {
            document.childByName("DecodedText").content.scriptAdaptor.actionSetText(Qt.atob(document.childByName("EncodedText").content.text))
        }
    }
}

image

Project file base64decode.pma (5.8 KB)

1 Like

Are all functions supported?
Поддерживаются все функции ?

Hello,

Thank you ! It worked :slight_smile:

I used this script (adapeted to my setup)

import QtQuick 2.0
Item {
    Connections {
        target: document.childByName("Button 2").scriptAdaptor
        onEventItemClicked: {
            document.childByName("Text 1").content.scriptAdaptor.actionSetText(Qt.atob(JSON.parse(document.childByName("test").content.httpText).payload))
        }
    }
}

It works but I noticed I always have to click twice on the “Button 2” for the decoded text to be displayed on “Text 1”

Now; I would like the decoded text to be refreshed on “Text 1” at a given interval.

I tried to use this script; but I’m stuck because I don"t now which event I should watch in order to start the “timer”
I need an event that is triggered when “Text 1” is displayed on the current page. Which one can I use ?
Is there a reference of available “events” somewhere ?

import QtQuick 2.12

Item {
    Connections {
        target: document.childByName("Text 1").scriptAdaptor
        onEventLoaded: {
            timer.start()
        }
    }
    Timer {
        id: timer
        interval: 3000
        onTriggered: document.childByName("Text 1").content.scriptAdaptor.actionSetText(Qt.atob(JSON.parse(document.childByName("test").content.httpText).payload))
    }
}

Thank you for your help :slight_smile:
Best regards

Hello,
In case it helps other people; I FINALLY got it working !

The code I’m using is:

import QtQuick 2.0
Connections {
    target: document.childByName("test").content.scriptAdaptor
    onEventHttpSuccess: {
        document.childByName("Text 1").content.scriptAdaptor.actionSetText(Qt.atob(JSON.parse(document.childByName("test").content.httpText).payload))
    }
}

For the refresh; I added an action on “Text 1” for triggering “test” HttpRequest every 1 sec
The action is triggered on “Text 1” event “Loaded”

For those like me, searching QML documentation: there is a “{…}” button (Copy generated script to clipboard) which is quite useful.

Best regards


Back to Flipabit >
Copyright © 2018. Flipabit Team. All rights reserved.