Friday, May 6, 2022

[SOLVED] Python: curl -d json will not work without apostrophe

Issue

I have to send the curl exact in that form to get a response in cli

curl -d '{"id":0,"params":["aa:aa:00:00:00:00",["mixer","volume","80"]],"method":"slim.request"}' http://localhost:9000/jsonrpc.js
curl -d '{"id":0,"params":["aa:aa:00:00:00:00",["playlist","play","/home/pi/mp3/File.mp3"]],"method":"slim.request"}' http://localhost:9000/jsonrpc.js

Running the command above in my script, I get a syntax error for the apostrophe

  File "./updateTimers.py", line 126
    strVolume = curl -d '{"id":0,"params":["aa:aa:00:00:00:00",["mixer","volume","80"]],"method":"slim.request"}' http://localhost:9000/jsonrpc.js
                                                                                                                                 ^
SyntaxError: invalid syntax

If I change apostrophes and quotation than my python script does not like the syntax also. Would be happy any advice. At least I need to run both commands one by one.

strVolume = curl -d '{"id":0,"params":["aa:aa:00:00:00:00",["mixer","volume","80"]],"method":"slim.request"}' http://localhost:9000/jsonrpc.js
strPlayMP3Command = curl -d '{"id":0,"params":["aa:aa:00:00:00:00",["playlist","play","/home/pi/adhan/mp3/Adhan-Makkah.mp3"]],"method":"slim.request"}' http://localhost:9000/jsonrpc.js

Solution

If I understand your question correctly, you want the curl -d ... to be a string in Python. To do this, you will also have to wrap curl in quotes: strVolume = "curl -d '{\"id\": 0, ...}' ...". Make sure you escape the quotes inside.



Answered By - Gareth Ma
Answer Checked By - Clifford M. (WPSolving Volunteer)