Author Topic: Running curl with os.execute  (Read 744 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

  • Full Member
  • ***
  • Posts: 209
  • Karma: +8/-3
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

  • Master Member
  • *******
  • Posts: 6131
  • Karma: +275/-69
  • "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

  • Full Member
  • ***
  • Posts: 209
  • Karma: +8/-3
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

  • Master Member
  • *******
  • Posts: 6131
  • Karma: +275/-69
  • "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.