Curl json request
#1
Hi i'm beginner in bash scripting and i just want your help to make the good curl json request to play random music

I use this request below in my internet browser and everything is good. Random music is played on my kitchen raspberry

http://192.168.1.12:8092/jsonrpc?request={"jsonrpc":"2.0","id":"kodi cuisine","method":"Player.Open","params":{"item":{"partymode":"music"}}}

But if i put this request in curl command line into a bash file nothing happen

A other problem is to put variable in the bash but i'm too bad


! /bin/bash
ipkodi=192.168.1.12
portkodi=8092
idkodi=kodi cuisine
curl "http://192.168.1.12:8092/jsonrpc?request={"jsonrpc":"2.0","id":"kodi cuisine","method":"Player.Open","params":{"item":{"partymode":"music"}}}"

Anyone can help me to modify this bash file ?
Thx for next answers
Reply
#2
PHP Code:
result=$( curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode"}}, "id": "kodi cuisine"}' -'content-type: application/json;' http://192.168.1.12:8092/jsonrpc) 

That will give you the return value in $result in case you want to check to see if it was successful or not.

To do it with variables

PHP Code:
ipkodi="192.168.1.12"
portkodi=8092
idkodi
="kodi cuisine"
result=$( curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode"}}, "id": $idkodi}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc)
echo $result 
Learning Linux the hard way !!
Reply
#3
(2016-09-13, 22:38)black_eagle Wrote:
PHP Code:
result=$( curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode"}}, "id": "kodi cuisine"}' -'content-type: application/json;' http://192.168.1.12:8092/jsonrpc) 

That will give you the return value in $result in case you want to check to see if it was successful or not.

To do it with variables

PHP Code:
ipkodi="192.168.1.12"
portkodi=8092
idkodi
="kodi cuisine"
result=$( curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode"}}, "id": $idkodi}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc)
echo $result 

Hi black_eagle thx for your help
i tried your solution but i have un error just like this:

{"error":{"code":-32700,"message":"Parse error."},"id":null,"jsonrpc":"2.0"}

i think is about idkodi variable because in colour line, this variable is'nt in red contrary to another variable. i try to put ' or " before or after this variable but nothing.I use nano to edit this script

have you a solution?
thx for support
Reply
#4
Oops !!

Sorry, I forgot to quote the variable. Sad

PHP Code:
ipkodi="192.168.1.12"
portkodi=8092
idkodi
="kodi cuisine"
result=$( curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode"}}, "id": '"$idkodi"'}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc)
echo $result 

You do not have to use that as an id though you know. you can just put a 1 there.

PHP Code:
ipkodi="192.168.1.12"
portkodi=8092
result
=$( curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode"}}, "id": 1}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc)
echo $result 

I think either of those should be OK.
Learning Linux the hard way !!
Reply
#5
hi it's me...............again

I tested this script but same message. Afte that i delete all space in result request and now i have this message

curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

and the script modification


#! /bin/bash
ipkodi="192.168.1.12"
portkodi=8092
result=$( curl -s --data-binary '{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"partymode"}},"id":1}'-H'content-type:application/json;'http://$ipkodi:$portkodi/jsonrpc)
echo $result

another idea perhaps -H parameter or variable ?
Reply
#6
PHP Code:
result=$(curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode":"music"}, "id": 1}}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc) 


^ I missed out the "music" bit !! My bad.....Should have read your 1st post more closely !!

However, although this time I have tested this call against 2 different instances of Kodi, it does not return an error, but neither does it return an 'OK'.
You can turn partymode on and off with
PHP Code:
curl ---data-binary '{ "jsonrpc": "2.0", "method": "Player.SetPartymode", "params":{"playerid":1, "partymode":true}, "id": 1}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc 

Replace true with false to turn it off again.
Learning Linux the hard way !!
Reply
#7
Wink 
(2016-09-15, 08:55)black_eagle Wrote:
PHP Code:
result=$(curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"partymode":"music"}, "id": 1}}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc) 


^ I missed out the "music" bit !! My bad.....Should have read your 1st post more closely !!

However, although this time I have tested this call against 2 different instances of Kodi, it does not return an error, but neither does it return an 'OK'.
You can turn partymode on and off with
PHP Code:
curl ---data-binary '{ "jsonrpc": "2.0", "method": "Player.SetPartymode", "params":{"playerid":1, "partymode":true}, "id": 1}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc 

Replace true with false to turn it off again.

Hi black_eagle
Sorry for my late answer but too busy these last days. So you're awesome !!!!!!! Big Grin you're script is just so good to start a random playlist, great !!!. But to turn off random playlist the "false" word doesn't work. That's why i use an other script and it works !!!! Smile

PHP Code:
#! /bin/bash
ipkodi=192.168.1.17
portkodi
=8093
curl 
---data-binary '{ "jsonrpc": "2.0", "method": "Player.Stop", "params":{"playerid":0}, "id": 1}' -'content-type: application/json;' http://$ipkodi:$portkodi/jsonrpc 

so now i can start and stop random playlist in my kitchen, bathroom, bedroom and livingroom with this script (replace variables) integrated in my domotic installation
thx for your patience and see you next time
Reply
#8
Cool Cool

Glad it's all working for you now. I should just point out though that you cannot rely on the player id always being the same and therefore some bash to get the correct id and stop it is as follows (note that $xbmcAddress & $xbmcPort need replacing with your ipkodi variables etc as this is a straight copy/paste from one of my scripts Wink)

PHP Code:
PlayerId=$(curl ---data-binary '{ "jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}' -'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
PlayerId=$(echo "$PlayerIdcut -d'{' -f3 sed 's/[^0-9]*//g')
result=$( curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.Stop", "params":{"playerid":'"$PlayerId"'}, "id": 1}' -'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc) 
Learning Linux the hard way !!
Reply

Logout Mark Read Team Forum Stats Members Help
Curl json request0