How to send HTTP Get or Post

Hello,
I am trying to get hubitat to send
http://IP:PORT/jsonrpc?request={"jsonrpc":"2.0","method":"System.Shutdown","id":1} to a device.

I am unsure on how to use rule machine to send this. Should I be using GET or POST? I have tried both, and the logs show that hubitat is doing something, but it doesn't actually seem to be doing anything at the end device. The above address works if entered into a web browser.

Any thoughts on this would be greatly appreciated.

That looks like a Get URL, but I’ve never seen JSON sent that way.

1 Like

I have it set up as a GET currently.

If it works from a web browser (assuming you mean putting into the location bar), you probably need GET

Try using %22 in place of the quotation marks in the query parameter.

1 Like

Sometimes putting what you want to send in a variable works better.

Thanks for the suggestions.
I tried adjusting the query parameter to say:
" http://IP:PORT/jsonrpc?request={%22jsonrpc%22:%22 2.0%22,%22method%22:%22System.Shutdown%22, "
This did not seem to make any change. It seems like when a person enters a %into this query, Hubitat interprets it as a variable.

I also tried making a local variable set as a string which has not worked either. Currently my rule looks like this:

For reference, the intent of this is to shutdown a PC running Libreelec.
https://forum.kodi.tv/showthread.php?tid=336134

Thanks for the suggestions so far, hopefully the additional information included can help pinpoint what I am missing.

1 Like

Are you sure you want that unpaired single quote (at the end) in there?

1 Like

No I'm not.
I did however try it with and without.

Try this, it just escapes all the special chars:

http://192.168.1.64:8080/jsonrpc%3Frequest%3D%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22System.Shutdown%22%2C%22id%22%3A1%7D
1 Like

No Joy.
Baby Reaction GIF

Try this: https://github.com/hubitat/HubitatPublic/blob/master/examples/drivers/httpGetSwitch.groovy

Install the driver code and create a new virtual device. Put your URL in the preferences setting for either on or off (or both) and flip the virtual switch.

Just to rule out any parsing weirdness with RM.

1 Like

I get error: warnCall to off failed: Illegal character in query at index 41: http://192.168.1.64:8080/jsonrpc?request={"jsonrpc":"2.0","method":"System.Shutdown","id":1}

which seems to be pointing to "{" I imagine this will be the issue with any special character. I tried using @mikes URL, but get a error 404.

That's strange that the URL-encoded string also failed.

Can you POST to the server instead with the JSON as the body? That's what I usually do with jsonrpc.

You lost me on that. I did try to only remove any special characters that threw an error at me and got this error:

[dev:527][debug][error:[code:-32700, message:Parse error.], id:null, jsonrpc:2.0]

[dev:527]debugSending off GET request to [[](http://192.168.1.64:8080/jsonrpc?request=%3D%7B%22jsonrpc%22.%222.0%22,%22method%22:%22System.Shutdown%22,%22id%22:1%7D)

EDIT: The error above is the same error I get when I enter [](http://192.168.1.64:8080/jsonrpc?request=%3D%22jsonrpc%22.%222.0%22,%22method%22:%22System.Shutdown%22,%22id%22:1%7D) into a browser location bar

I think I've narrowed it down to { and } special characters.

http://192.168.1.64:8080/jsonrpc?request=%7B%22jsonrpc%22:%222.0%22,%22method%22:%22System.Shutdown%22,%22id%22:1%7D

Using the Driver @tomw recommended did the trick. Thanks to All who replied.

1 Like

You could make a helper like this, depending on how many commands you need to support:

def query = [jsonrpc:"2.0", method:"System.Shutdown", id:1]
def json = new groovy.json.JsonOutput().toJson(query)
def enc = java.net.URLEncoder.encode(json)
    
def URI = "http://192.168.1.64:8080/jsonrpc?request=" + enc
1 Like