How to make HTTP GET work?

FWIW the bug seems to be handling null characters:

From the hubitat logs:

Action: Send GET to: http://pi4.home:5005/Master%20Bedroom/say/Food%20is%20ready.%20Come%20down%20now.

From the sonos API logs, it gets a mangled request:

2020-01-13T23:35:23.633Z ERROR
  { error: 'action \'masternull20isnull20comenull20now.\' not found' }

Great suggestion, thanks @tomw Here's the result:

URL in Rules Machine: http://pi4.home:5005/Master%20Bedroom/say/Food%20is%20ready.%20Come%20down%20now.

HTTP Request URI: /Masternull20isnull20Comenull20now.

It looks like Rules machine is mangling the % character, maybe because it's trying to do variable substitution? @bravenel what do you think?

EDIT: I can't post due to the first day limit, but here's the platform version @tomw

Hubitat Elevation® Platform Version

2.1.8.115

Good catch.

What hub firmware version are you running? I am on 2.1.8.115, and URLs with null characters (obviously encoded as "%20") still work in my custom drivers. So, I agree that this seems like a Rule Machine bug of some sort.

2.1.8.115

@aaiyar Bump? Any suggestions for next steps? Email support?

1 Like

Yup. Might also want to report this a possible bug in the parent RM 4.0 thread. I know @bravenel watches that thread - because he found a solution when I found something inconsistent a few months ago.

There is a problem with URL encoding for URLs in RM. Unfortunately, the % character is overloaded in this context, and that is causing the problem. I will take a look at it.

There is a simple work around for this:

  1. Never put a URL encoded string directly into a URL field in Rule Machine. This is due to the use of % as a delimiter in RM for various things, including variable references.

  2. If you want to put a URL encoded string into a URL in Rule Machine, put it in a String Variable at the time you create the variable, then use that variable in the URL field with %variable-name%.

Going forward, there are two ways we could possibly address this:

a. We add a URL Encode means of setting a String Variable. You would put the non-URL encoded string into that action, and RM would encode it into the String Variable.

b. We add a "literal" means of setting a String Variable, that does not apply the %xxx% transforms to the string being set into the variable in that action, thus allowing a URL encoded string to be put into a variable.

@bravenel

Having fun getting this to work, I’m trying to issue a HTTP get to this url (user_id substituted with dummy value).

I’ve tried:

https://virtual-device.bespoken.io/process?message="{{ ask neato to clean downstairs }}"&user_id= user_id=alexa-11111-11111-1111-1111-111111111111

And encoding:

https://virtual-device.bespoken.io/process?message="{{%20ask%20neato%20to%20return%20to%20dock%20}}%22&user_id=alexa-11111-11111-1111-1111-111111111111

I’ve tried setting the encoded and non encoded version as a variable and putting %varname% in the RM URL parameter.

How do I capture this encoded url in a HTTP get in RM?

Cheers

Put the non-encoded URL in a variable. Then use Set Variable with Replace to replace " " with "%20". Then use that variable in the HTTP Get action.

Or, change the encoded URL to use some substitute for %, such as ~, and put that in a variable. Then use replace for "~" to "%", and use that variable in the HTTP Get.

1 Like

There is a "URL Encode switch for rules. Exactly what does it do. Does it look for the ? and only encode after it?

Where can I find these logs on my Sonos API server?

Has this problem been fixed? URLs that execute as expected in my browser don't work in RM.

From the 2.2.6 Release Notes,

"Added URL Encode option for Send http GET and Send http POST. There is an overload of the % character between things like %device% and URL encoding, so the URL encoding is applied after those %device% substitutions are done. There is also a new action added to URL encode into a string variable."

If I'm understanding correctly, the 'URL Encode' switch should be used when our URL contains a Hubitat variable? Are there other scenarios when we would need to use this switch?

If this is not correct, can we get a better explanation and possibly an example of how the 'URL Encode' switch works?

URL encoding and variables in strings are two different things. What you quoted is simply pointing out that the sequence of events is to first expand %variable-name%, and then URL encode the resulting string, when URL encoding of the entire URL is selected. Space characters and other special characters generally do not work in URLs, hence the need for URL encoding.

1 Like

Maybe it's the head cold I'm fighting but this is still not clear.

'URL Encode' is a switch. Doesn't this mean the entire URL is always selected or can we highlight just a portion of the URL and then choose the 'URL Encode' switch?

Do you mean something like, "Hubitat doesn't gracefully handle URLs with special characters. Therefore, if your URL contains special characters, please use the 'URL Encode' switch. Additionally, since % is used for Hubitat variable substitution, please use the 'URL Encode' switch rather than manually encoding special characters. e.g. %24 for $."

1 Like

No, URLs can’t have special characters in them.

3 Likes

There is variable substitution? Can you give me a URL for the documentation? Thanks.

@bravenel Thank you for fixing this.

Okay, if URLs can't have (unencoded) special characters and Hubitat uses % for variables, then we should always use:

http://192.168.0.237:5005/guest room/favorite/10 Hour Lawnmower/

with the 'URL encode' switch rather than:

http://192.168.0.237:5005/guest%20room/favorite/10%20Hour%20Lawnmower/

Correct?

Yeah, I think so. The problem was that putting things like %20 ...%20 in a url would collide with the use of %variable-name%, and the url would be trashed. That's why I said above that the variable substitution happens before the url encoding, so that the %variable-name% strings have all been converted to the variable values. The URL encoding will convert "10 Hour Lawnmower" to "10%20Hour%20Lawnmower", so you don't want to have %20 in the string before it is encoded.

Perhaps the action should display the url it's going to send.

2 Likes