sendHttpPostRequest with Blockly

I have selfhosted ntfy for push-messages, and i also want to use it with openhab.

This code works:

var headers={"Authorization": "Basic xxxxxxxxxxxxxxxx", "Content-Type": "application/json", "Title" : "openhAB", "Tags" : "+1, -1, warning"};
var url = "https://ntfy.xxxxxx.de/openhab"
var content = 'Hallo'
actions.HTTP.sendHttpPostRequest(url,"application/json", content,headers, 5000)

Now i tried to do it with Blockly:

The code looks very similar, but does not work:

var content, headers, url;


content = 'Hallo';
headers = '{"Authorization": "Basic xxxxxxxxxxxxxxxx", "Content-Type": "application/json", "Title" : "openhAB", "Tags" : "+1, -1, warning"}';
url = 'https://ntfy.xxxxxxx.de/openhab';
actions.HTTP.sendHttpPostRequest(url,"application/json", content,headers, 5000)

Only a few differences with ’ and ", but is there a way to do it right with blockly?
Thanks!

There’s a recent PR to add this support to Blockly. See [blockly] Provide HTTP request block by stefan-hoehn · Pull Request #2411 · openhab/openhab-webui · GitHub. It should be in 4.2 M2 and the latest snapshots of course.

My recommendation would be to put it all into an inline script using the JS you know works, or upgrade and use the new block.

The difference between the two versions of the code is headers. In the working version you’ll notice that it’s a JavaScript dict. In the Blockly version it’s a String. You could fix this in the Blockly by using the map blocks to build up the dict with the name/value pairs.

1 Like