This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Http POST body
#1
Hi @ all,
I need a little help for send a body in a script. I don't know way, if i send it via Postman it works and if I try via script, the answer it is 422 (error in body compose).
This is my script:
Code:
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')
--local body = {}
test = '{"commands":{["component":"main","capability":"switch","command":"on"]}}' --body json
--log(test)
token = 'xxxxxxxxxxxxxxxxxxxxxx' --token auth
tbl = {}

res, code,headers, status = https.request({
  url = 'https://api.smartthings.com/v1/devices/--devices ID--/commands',
  method = 'POST',
  headers = {
    ['authorization'] = 'Bearer ' ..token,
    ['content-type']  = 'application/json',
    ['content-length'] = #test,
  },
  sink = ltn12.sink.table(tbl),
    log(sink),
  source = ltn12.source.string(test),
    log(source)
})

if res and code == 200 then
  resp = table.concat(tbl)
  resp = json.pdecode(resp)

  -- This lists a Table with all your information: 
  log(resp)
else
  log(res, code)
end
Reply
#2
Have you replaced "--devices ID--" with actual device id in the request URL?
Remove log calls from inside the request table.
Your JSON is incorrect, use json.encode to have a valid output:
Code:
test = json.encode({
  commands = {
    {
      component = 'main',
      capability = 'switch',
      command = 'on',
    }
  }
})
Reply
#3
(02.02.2023, 08:29)admin Wrote: Have you replaced "--devices ID--" with actual device id in the request URL?
Remove log calls from inside the request table.
Your JSON is incorrect, use json.encode to have a valid output:
Code:
test = json.encode({
  commands = {
    {
      component = 'main',
      capability = 'switch',
      command = 'on',
    }
  }
})
Hi admin,
Thanks, I got.
I will try and I let you know.
Best regards Cristian
Reply
#4
(02.02.2023, 08:29)admin Wrote: Have you replaced "--devices ID--" with actual device id in the request URL?
Remove log calls from inside the request table.
Your JSON is incorrect, use json.encode to have a valid output:
Code:
test = json.encode({
  commands = {
    {
      component = 'main',
      capability = 'switch',
      command = 'on',
    }
  }
})
Hi admin,
goods it works fine.
Thank you so much.
BR Cristian
Reply


Forum Jump: