Logic Machine Forum
Http POST body - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Http POST body (/showthread.php?tid=4545)



Http POST body - CristianAgata - 01.02.2023

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



RE: Http POST body - admin - 02.02.2023

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',
    }
  }
})



RE: Http POST body - CristianAgata - 02.02.2023

(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


RE: Http POST body - CristianAgata - 02.02.2023

(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