02.10.2025, 18:44
I receıved below 422
Here is the full code
Here is the working postman cURL
Thank you for the support
Code:
* string: Request body: {"commands":[{"capability":"switch","command":"on","component":"main"}]}
* string: SmartThings response code: 422
* string: SmartThings response body: {"requestId":"391879646930462279","error":{"code":"ConstraintViolationError","message":"The request is malformed.","details":[{"code":"BodyMalformedError","target":"httpRequestBody","message":"The request body is malformed and cannot be processed by server.","details":[]}]}}
Here is the full code
Code:
local https = require('ssl.https')
local json = require('json')
local ltn12 = require('ltn12')
-- ️ put your smartthings pat here
local token = '*************'
local livingr_cam_id = '**************************'
-- Camera KNX group objects (choose addresses as needed)
local ga_cam_onoff_set = grp.create({ datatype = dt.bool, address = '33/4/1', name = 'Camera On/Off' })
local ga_cam_onoff = grp.create({ datatype = dt.bool, address = '33/4/2', name = 'Camera On/Off Status' })
local ga_cam_motion = grp.create({ datatype = dt.bool, address = '33/4/11', name = 'Camera Motion Detected' })
local ga_cam_sound = grp.create({ datatype = dt.bool, address = '33/4/12', name = 'Camera Sound Detected' })
local cam_set = grp.getvalue(ga_cam_onoff_set) -- Set
local cam_set_status = grp.getvalue(ga_cam_onoff) -- Status
-- Helper to turn camera on or off using a raw JSON string
local function set_camera_onoff(cam_id, state)
local cmd = state and 'on' or 'off'
local body = require('json').encode({
commands = {
{
component = 'main',
capability = 'switch',
command = cmd,
}
}
})
log('Request body: ' .. body)
local sink_tbl = {}
local res, code = https.request({
url = 'https://api.smartthings.com/v1/devices/' .. cam_id .. '/commands',
method = 'POST',
headers = {
['authorization'] = 'Bearer ' .. token,
['content-type'] = 'application/json; charset=utf-8',
['accept'] = 'application/json',
},
source = ltn12.source.string(body),
sink = ltn12.sink.table(sink_tbl),
})
local response_body = table.concat(sink_tbl)
log('SmartThings response code: ' .. tostring(code))
log('SmartThings response body: ' .. response_body)
return res, code, response_body
end
if cam_set == true then
if cam_set_status == false then
set_camera_onoff(livingr_cam_id, true)
end
elseif cam_set == false then
if cam_set_status == true then
set_camera_onoff(livingr_cam_id, false)
end
end
Here is the working postman cURL
Code:
curl --location 'https://api.smartthings.com/v1/devices/***************************/commands' \
--header 'Content-Type: application/json' \
--header 'Authorization: **********' \
--data '{
"commands": [
{
"component": "main",
"capability": "switch",
"command": "on"
} ]
}'
Thank you for the support