balsimpson / node-red-contrib-shortenurl

Shorten long URLs using the is.gd service.
0 stars 1 forks source link

Output fails if payload doesn't already exist... #1

Open Swiftnesses opened 5 years ago

Swiftnesses commented 5 years ago

Hi,

I have a few cases whereby there is no payload when I send the shorturl to the node, in this case it fails... I workaround it by creating a fake payload before, but it's not ideal :)

I believe this issues is because you try to send the payload using "msg.payload.shorturl", which will not work if no payload exists (annoying, I know). Instead I guess we need:

msg.payload = '{"shorturl:" "url"}'

Edit: It would be really nice if we could configure the input msg name and the output, would help avoid conflicts in more complicated flows.

balsimpson commented 5 years ago
                        if (msg.longurl) {
                longurl = msg.longurl;
            } else if (msg.payload && msg.payload.longurl) {
                longurl = msg.payload.longurl;
            } else {
                longurl = config.longurl;
            }
            // SHORT URL
            if (msg.shorturl) {
                shorturl = msg.shorturl;
            } else if (msg.payload && msg.payload.shorturl) {
                shorturl = msg.payload.shorturl;
            } else {
                shorturl = config.shorturl;
            }

This is my code. I am checking if there is msg.payload before I assign it to a variable. If there is no payload, you should get the shorturl in msg.shorturl. Does that answer your problem? Do let me know.

Swiftnesses commented 5 years ago

Hi @balsimpson it does.

I also noticed that urls such as "https://qft.hodloo.com/#/binance:eth-usdt" do not shortnen correctly and return only "https://qft.hodloo.com", it works fine via is.gd direct...

Swiftnesses commented 5 years ago

Seems to be an API issue, works direct, but not via the API GET, hmmm.

is.gd documentation states: The url parameter is the address that you want to shorten. You must URL encode this parameter before submitting it otherwise your application will not properly support URLs containing symbols such as hash, semicolon, plus and ampersand (among others).

A lot of languages have a built in function for URL encoding such as encodeURIComponent() in Javascript, urllib.quote() in Python and urlencode() in PHP.

Swiftnesses commented 5 years ago
            // LONG URL
            if (msg.longurl) {
                longurl = encodeURIComponent(msg.longurl);
            } else if (msg.payload && msg.payload.longurl) {
                longurl = encodeURIComponent(msg.payload.longurl);
            } else {
                longurl = config.longurl;
            }
balsimpson commented 5 years ago

Have updated the code. Do check if it's working.