Running curl with os.execute

I have a curl command that I am trying to run in a scene. I’ve SSHed into the Vera and run the Curl command successfully, but whenever I try to move it into a scene calling curl via the os.execute I receive the error “failed attempt to call a nil valuee[0m <0x74174520>”

os.execute(“curl -k https://api.twilio.com/2010-04-01/Accounts/censored/Calls.json -u ‘censored:censored’ --data-urlencode ‘From=5617553581’ --data-urlencode ‘To=3057557687’ --data-urlencode ‘Url=http://twimlets.com/message?Message[0]=This%20is%20Premier&’”)

[quote=“Floridagator, post:1, topic:198462”]I have a curl command that I am trying to run in a scene. I’ve SSHed into the Vera and run the Curl command successfully, but whenever I try to move it into a scene calling curl via the os.execute I receive the error “failed attempt to call a nil valuee[0m <0x74174520>”

os.execute(“curl -k https://api.twilio.com/2010-04-01/Accounts/censored/Calls.json -u ‘censored:censored’ --data-urlencode ‘From=5617553581’ --data-urlencode ‘To=3057557687’ --data-urlencode ‘Url=http://twimlets.com/message?Message[0]=This%20is%20Premier&’”)[/quote]

I had the same problem and found the answer by testing on my own linux box.

use a single quote for the whole command and the URL in double quotes.

Example:
os.execute(‘curl -X GET “http://192.168.254.38/api/v1/commands/?cmd=playplaylist&name=panic”’)

There’s a single quote before the curl command and another one at the end of the statement after the double quote.

1 Like

Great answer! Thanks for sharing.

For completeness, Lua also has a further string quoting mechanism, which allows you to embed both single and double quotes in a string.

S = [[ both 'single' and "double" quotes in a string ]]

There’s even an extension of this which allows [[ or ]] to be in a quoted string, but I’ll leave that as an exercise for the reader!

The brackets are good to know. I just got a Vera Plus, so now time to learn Lua… :o

Thanks for the reply! Wish there was an easier way to test LUA code on the Vera.

At least two easy ways:

[ul][li]Rex Beckett’s excellent LuaTest http://forum.micasaverde.com/index.php/topic,24018.0.html [/li]
[li]amg0’s fantastic AltUI plugin http://forum.micasaverde.com/index.php/board,78.0.html which does so much more as well[/li][/ul]

Both the above allow you to use the print command to see output, which Vera’s native Test Lua window does not.

For people in the future who come here looking for a way to execute ‘curl’ in their Vera with JSON… I was able to figure it out when I was trying to get Slack webhooks to work. This worked for me. Notice how the quotes need to be escaped in just the right way.

os.execute(‘curl -X POST -k -H 'Content-type: application/json' -d '{"text":"hello world"}' https://hooks.slack.com/services/AAAAAA/BBBBB/CCCCCC’)

For people in the future who come here looking for a way to execute ‘curl’ in their Vera with JSON… I was able to figure it out when I was trying to get Slack webhooks to work. This worked for me. Notice how the quotes need to be escaped in just the right way.

os.execute(‘curl -X POST -k -H 'Content-type: application/json' -d '{"text":"hello world"}' https://hooks.slack.com/services/AAAAAA/BBBBB/CCCCCC’)

I try to run

os.execute(‘curl -s --data-binary ‘{“jsonrpc”:“2.0”,“id”:“1”,“method”:“Player.Open”,“params”:{“item”:{“file”:“special://profile/playlists/music/f.m3u”}}}’ -H ‘Content-type: application/json’ http://192.168.1.5:8080/jsonrpc’)

but it does not work in any way?
Can anybody help me to correct the code?

Click the first link below.

Also, please don’t cross-post the same question to multiple topics. It isn’t necessary and makes the threads harder to follow for future users trying to get the same solution.

1 Like

Thanks a lot.

Sure, I will not do that again.