How to make HTTP GET work?

@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

I would say so but more importantly, the Logs would display the actual URL that was sent.

I've followed your guidance but the GET fails. This is what I see in my logs:

Is there a way to capture or log the JSON or raw data of the response?

You can put the response into a String variable. There is an option when setting a String variable to put the response for HTTP Get in it.

Will the 'Set from HTTP GET response' function automatically encode the given URL? There isn't a 'URL encode' switch.

image

No, but you can URL encode into a String variable. Just the part you need encoded, and then use %variable-name% to include that in the url.

Sure, I could but that doesn't test the HTTP GET with 'URL encode' switch that isn't working for me.

But you can do it the same for the HTTP GET. So that the string put into the http get to get the response, is the same as the string without getting a response. Isn't the idea to find out what response you're getting?

No, the idea is to find out why Hubitat's HTTP GET function isn't working.

If I put the URL into a variable, everything works as expected. If I use the 'URL encode' switch, the GET fails.