Send a Script to a Shelly relay

Hi all,

I've been struggling to get a piece of script from HE over to a Shelly relay as per their documentation but I can't manage to translate the Python example to Groovy, any idea what I'm doing wrong ?

def uploadScript(){
    code = "Shelly.addStatusHandler(function(event) {if ( === \"switch\"){let url = \"http://HUBITAT_IP:39501/switch/\" + JSON.stringify( + \"/\" + (\"on\":\"off\") + \"/\";\"HTTP.GET\", {\"url\": url});}});";
    uri = "http://${shelly_ip}/rpc/Script.PutCode"
    def postParams = [
        uri: uri,
        requestContentType: 'text/html',
        contentType: 'application/octet-stream',
        body: ['code' :, 'id': 1, 'append': false]
The original request as per
req = {"id": id_, "code": data, "append": append}
req_data = json.dumps(req, ensure_ascii=False)
res =, data=req_data.encode("utf-8"), timeout=2) res.json()
*/ postParams
        asynchttpPost('myCallbackMethod', postParams)
    }catch (e){
        log.debug e.message

def myCallbackMethod(response, data) {
    log.debug "status of post call is: ${response.status}"

Message is always 'status of post call is: 500' aka Internal Server Error

The shelly examples are available in bash/curl and python (shelly-script-examples/tools at main · ALLTERCO/shelly-script-examples · GitHub), but neither got me any closer to the finish line.

This is how print(req_data.encode("utf-8")) on the python side looks:

{"id": "1", "code": "Shelly.addStatusHandler(function(event) {if ( === \\\\\\"switch\\\\\\"){let url = \\\\\\"http://HUBITAT_IP:39501/switch/\\\\\\" + JSON.stringify( + \\\\\\"/\\\\\\" + (\\\\\\"on\\\\\\":\\\\\\"off\\\\\\") + \\\\\\"/\\\\\\";\\\\\\"HTTP.GET\\\\\\", {\\\\\\"url\\\\\\": url});}});", "append": false}