Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Brain melt.......encode url - how to ?


AutoFrank

Question

Hi,

I am leveraging a bridge to interact with my sonos systems

 

The command to invoke is simple....

Please login or register to see this code.

but if I want to have more than one word i need to separate them with a %20 in the url ..like

Please login or register to see this code.

Now if I drop the following url into a browser (note space between 'hi' and 'there')

Please login or register to see this code.

It works !!!...

..but if i use it in a scene it doesn't

Please login or register to see this image.

/emoticons/default_icon_sad.gif" alt=":-(" />

 

I had this working from a scene before so I asked the developer of the sonos bridge and he said ..

 

 

 

That's regular url encoding. Most browsers will convert that transparently when writing a URL, but if you invoke it from a script or similar, you need to do that, and always had to (unless the script url encode automatically).

 

the last part is the important bit.....so my question is how can I get the http request in the scene to invoke the url so I don't have to add the %20 as spaces

I tried a number of options but can only seem to find a way to encode headers or data but not the url

 

I am 99% sure I had this working but in a recent overzealous clean up of my code I must have deleted the sample code that worked for me.

 

Could anybody help ?

 

Thanks

-Frank

 

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

try this function:

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0

Please login or register to see this code.

function urlencode(str)

  if (str) then

      str = string.gsub (str, "([^%w])",

      function (c) return string.format ("%%%02X", string.byte(c)) end)

  end

  return str 

end

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Please login or register to see this code.

     

     

    okay - must be missing something here

    looking to encode the string "the cat sat" to "the%20cat%20sat"

     

    Whats not right here ?

    Please login or register to see this code.

    and I'm getting the following output

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    Please login or register to see this code.

    local message = "the cat sat on the mat"

    fibaro:debug("original message = " .. message)

    function urlencode(s)

    if (s) then

    s = string.gsub (s, "\n", "\r\n")

    s = string.gsub (s, "([^%w ])",

    function (c) return string.format ("%%%02X", string.byte(c)) end)

    s = string.gsub (s, " ", "+")

    end

    return s

    end

    test = urlencode(message)

    fibaro:debug("encoded message = " .. test)

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Please login or register to see this code.

    Thanks Remko

     

    I thought I could use the same variable after it's encoded but looks like you can't

    much appreciated 

     

    -F

    Link to comment
    Share on other sites

    • 0

    of course you can, just do

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

     

    of course you can, just do

    Please login or register to see this code.

    Yep, but then it was less clear what the issue was

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Yep, but then it was less clear what the issue was

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

     

    might have been my fat fingers

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

     

    thanks for the help

    -F

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...