[SOLVED] Sending Post with a JSON payload?

I ended up using BAT file and send the text as paramater INside I have curl command. Also one critical thing in each BAT including the one for starting OH is to have

chcp 65001

at the start. This force unicode and I can send Cyrilic now.

Your curl example strips the " but maybe the server does not care, I tested lot of combination using a small http server for debugging the requests and it was not working.

1 Like

Hi All,

I can successfully send this request but im confused as to how to link it to a switch.

Can anyone point me to an example please?

curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"Player.SetSubtitle","params":{"playerid":1,"subtitle":"off"}}'

Thank you

I think you cannot add the HTTP header when using item configuration/HTTP binding
(anyone to prove me wrong is welcome, it’s long ago I tried so things may well have changed).
Here’s an example to do it with a rule:

rule "Landroid command"
when
        Item Landroid_Command received command
then
        val String URL = "http://admin:0000@landroid/jsondata.cgi"

        var String jsondata = 'data=[[\"settaggi\",' + Landroid_Command.state.toString + ',1]]'

        logInfo("rules", "Sending Command " + Landroid_Command.state + " to Landroid.")

        sendHttpPostRequest(URL, "application/x-www-form-urlencoded", jsondata)
end

Hello Markus

Thank you.

Am I right in saying the json data is the part between ‘’ ?

E.g:

rule "Turn Kodi Subtitle Off"
when
        Item myKodi_subtitle_off received command
then
        val String URL = "http://192.168.0.12:8080/jsonrpc"

        var String jsondata = '{“jsonrpc”:“2.0”,“id”:1,“method”:“Player.SetSubtitle”,“params”:{“playerid”:1,“subtitle”:“off”}}' + myKodi_subtitle_off.state.toString + ',1]]'

        logInfo("rules", "Sending Command " + myKodi_subtitle_off.state + " to myKodi_subtitle_off.")

        sendHttpPostRequest(URL, "Content-Type: application/json", jsondata)
end

Yes.

BTW I saw in the docs that you can do it using items configuration, see a section on HTTP headers.

OK, ill try using the rule and failing that investigate more in the Docs on your suggestion. Thank you Markus

Thanks @mstormi that did the trick!

Works beautifully!

Solution:


rule "Turn Kodi Subtitle Off"
when
        Item myKodi_subtitle_off received command OFF
then
        val String URL = "http://192.168.0.12:8080/jsonrpc"
        var String contenttype = "application/json"
        var String jsondata = '{"jsonrpc":"2.0","id":1,"method":"Player.SetSubtitle","params":{"playerid":1,"subtitle":"off"}}'

        logInfo("Kodi subtitle", "Sending Command " + myKodi_subtitle_off.state + " to myKodi_subtitle_off.")

        sendHttpPostRequest(URL, contenttype, jsondata)
end


rule "Turn Kodi Subtitle On"
when
        Item myKodi_subtitle_on received command ON
then
        val String URL = "http://192.168.0.12:8080/jsonrpc"
        var String contenttype = "application/json"
        var String jsondata = '{"jsonrpc":"2.0","id":1,"method":"Player.SetSubtitle","params":{"playerid":1,"subtitle":"on"}}'

        logInfo("Kodi subtitle", "Sending Command " + myKodi_subtitle_on.state + " to myKodi_subtitle_on.")

        sendHttpPostRequest(URL, contenttype, jsondata)
end

2 Likes