Doing a PUT command in LUA?

rigpapa,

I cut and pasted what you did and got the error message -

“[string “ALTUI - LuaRunHandler”]:7: ‘)’ expected (to close ‘(’ at line 5) near '” -m ‘… timeout …’“'”

But that’s OK. Breaking it apart into separate lines made the issue clear. I understood what you did (those damn quotes!), plugged the correct form into the original one line of code and it worked like a charm!

thanks again for all the help, I certainly couldn’t have gotten to this point without your help!

Think you missed something at the end of this line.
" -H 'Content-Type: application/json'" ..

Indeed! Corrected in situ.

Try that copy-paste again.

1 Like

Yup, the correction did the trick. Thanks akbooer.

The human “readability” of the string concatenation approach is much better.

And BTW, it’s heartening to see that even the “pros” can make errors in this stuff! I won’t feel quite as dumb the next time I spend hours troubleshooting a bit of code that doesn’t work, only to find out I misplaced a comma, or misspelled a variable!

If you want readability…

local timeout = 5
local shadeNUM = 42654     
local position = 7943
local url = "192.168.1.28"

local json = require "dkjson"
local Lua = {shade = {id = shadeNUM, positions = {position1 = position, posKind1 = 1}}}
local JSON = json.encode (Lua)

local command = "curl -s -X PUT -H 'Content-Type: application/json' -m %d -d '%s' http://%s/api/shades/%d"

os.execute (command: format (timeout, JSON, url, shadeNUM))

…well, I think it’s readable!

They are, in the end, only human. :grinning:

1 Like

Agreed. At some point, when the JSON payload is complex enough, it’s worth bringing in the JSON library letting it do some heavy lifting.

A post was split to a new topic: Controlling Smith & Noble Shades?