Help With httpPost JSON and Sonos Speaker


(John Rubin) #1

Hey everyone. It’s been a very long time since I’ve attempted to write a SmartApp and am having trouble with how to begin, let alone the code necessary to accomplish it.

I’m trying to write an app that monitors the Now Playing field (trackDescription) of a specific Sonos speaker and whenever it changes, to send off that value of the now playing track as a string via an HTTP Post request to another web service I have running. The goal is to show the now playing track at all times on a separate display that is near the speaker. The web service works no problem and I can manually trigger http requests with hardcoded strings and it displays with no issue.

The HTTP request I need to send out is (the access keys and URI are not real, just need to understand the syntax):

URL:

https://somewebservice.com/api/v1/dev/widget/update/com.webservice.a6a323440f15fabab44392352343245

Headers:

accept: application/json
X-Access-Token: YjE4ZWga3M2NmYmI1YTcyZjFkYmI3ZWVjMzY0OThkZjJiMaeHDJWQ2NTIwMDY2ZTQwYWUxNmNiN2I0MGQ5ODE2Mw==
Cache-Control: no-cache

Body:

{
“frames”: [
{
“text”: " Now Playing",
“icon”: “null”
},
{
“text”:" Song Title - Song Band", //<-----now playing track string goes here
"icon":“null”
}
]
}

Any help here would be absolutely completely appreciated. It is a very simple app in that when a value changes, to just send off that value in a webhook, but I can’t for the life of me even think about where to begin, let alone the syntax of how to write it.

Thanks everyone!


( I hate Mondays) #2

If you look into CoRE’s source code (search for something like vcmd_makeHttpRequest) you will find an example using JSON.

The body needs to take a Map like

def body = [ frames: [ [text: "Now playing", icon: null], [text: "Something else", icon: null] ]

PS I am on a mobile phone and can’t search code ;(


(John Rubin) #3

Thank you very much for that. I was able to get the http request to fire off successfully. I’m still having some difficulty subscribing to the speaker to get the Now Playing track.

Is there a way to subscribe to that like you can with switches, etc.? I can get the now playing track, but it only gets it once and when the song changes it doesn’t update.

Thanks again for the httppost help!


( I hate Mondays) #4

Subscribe to the device, go for the “status” attribute, I assume? It should work just like the “switch”…