Author Topic: Running curl with os.execute  (Read 1048 times)

Offline Floridagator

  • Sr. Newbie
  • *
  • Posts: 42
  • Karma: +0/-0
Running curl with os.execute
« on: January 31, 2018, 10:27:56 am »
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 value <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%5B0%5D=This%20is%20Premier&'")

Offline jeubanks

  • Beta Testers
  • Full Member
  • *****
  • Posts: 224
  • Karma: +11/-4
Re: Running curl with os.execute
« Reply #1 on: February 16, 2018, 01:25:34 pm »
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 value <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%5B0%5D=This%20is%20Premier&'")

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.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6201
  • Karma: +276/-70
  • "Less is more"
Re: Running curl with os.execute
« Reply #2 on: February 16, 2018, 01:51:29 pm »
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.

Code: [Select]
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!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline jeubanks

  • Beta Testers
  • Full Member
  • *****
  • Posts: 224
  • Karma: +11/-4
Re: Running curl with os.execute
« Reply #3 on: February 16, 2018, 03:57:30 pm »
The brackets are good to know.  I just got a Vera Plus, so now time to learn Lua....  :o

Offline Floridagator

  • Sr. Newbie
  • *
  • Posts: 42
  • Karma: +0/-0
Re: Running curl with os.execute
« Reply #4 on: February 20, 2018, 08:46:41 pm »
Thanks for the reply! Wish there was an easier way to test LUA code on the Vera.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6201
  • Karma: +276/-70
  • "Less is more"
Re: Running curl with os.execute
« Reply #5 on: February 21, 2018, 02:57:23 am »
Thanks for the reply! Wish there was an easier way to test LUA code on the Vera.

At least two easy ways:

Both the above allow you to use the print command to see output, which Vera's native Test Lua window does not.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline piveraproject

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Re: Running curl with os.execute
« Reply #6 on: October 07, 2018, 05:57:28 pm »
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')

Offline piveraproject

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Re: Running curl with os.execute
« Reply #7 on: October 07, 2018, 05:58:42 pm »
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')