How to format JSON for HTTP POST – 🛎️ Get Help – Hubitat


Hi folks –

Basic question here, but I’m having trouble figuring out how to properly format a message to send to a ntfy service via HTTP post. There are numerous methods and examples (link below) on how to publish a message, but I don’t know how these various formats translate to the HTTP POST page in Hubitat (action in RM). It seems I would need the ability to specify header fields. However there are so many publish options i’m hoping someone can point me to another way?

Can anyone help me to format basic parameters in the POST such as “message”, “priority”, etc? The below test action in RM works so I know the service is running properly. Thank you kindly!

https://docs.ntfy.sh/publish/

That body is actually text, not JSON. Or were you simply confirming that the service is up and listening for requests?

That’s correct the screenshot was just to show it does work at a basic level. However there are many more parameters (other than just the message body) that I’d like to set, like priority, title, etc. So I’m unsure on how to do that more “advanced” formatting. Either JSON,… I see you can format a webhook (didn’t seem to work), etc just looking for some formatting help in any of the supported formats that someone may be comfortable offering advice on.

Like most things, there’s many ways to do what you want. Here’s an example from my Android TV notification driver:

void deviceNotification(String text, String title) {

  def headers = [:];
  headers.put("Content-Type", "application/json");
  def bodyJson = "{\"duration\": \"${DisplayDuration}\", \"position\": ${Position}, \"title\": \"${title}\", \"titleColor\": \"${TitleColor}\", \"titleSize\": ${TitleSize}, \"message\": \"${text}\", \"messageColor\": \"${MessageColor}\", \"messageSize\": ${MessageSize}, \"backgroundColor\": \"${BackgroundColor}\" }";
  def postParams = [ 
        uri: "http://${TVIPAddress}:7979/notify",
        headers: headers,
        contentType: 'application/json',
        body : bodyJson
        ]


    try {

        httpPost(postParams)
        { resp -> 
            debugLog("deviceNotification: ${resp.data}")

        }

        }
        catch (Exception e) {
        errorLog("deviceNotification: Unable to send notification: ${e}")
        }
}



Source link

Leave a Comment