I am struggling a litte bit with understanding what is going wrong here:
The server throws an error when using this function. The response is not helpful as it seems like an internal server error.
I am guessing it has something to do with the payload, as the curl opposite of this function works fine:
Is the payload formatted correctly as in the curl?
Code:
function set_light_state(id, is_on)
local url = base_url .. 'devices/' .. id
local payload = json.encode({
{
attributes = {
isOn = is_on
}
}
})
response_body = {}
local _, code = https.request{
url = url,
method = "PATCH",
headers = {
["Authorization"] = "Bearer " .. access_token,
["Content-Type"] = "application/json"
},
source = ltn12.source.string(payload),
sink = ltn12.sink.table(response_body),
verify = false
}
-- Check respnse
if code == 200 then
log('Light was set to ' .. (is_on and 'på' or 'av'))
else
log('Error updating light state. Status code: ' .. tostring(code))
log('Server response: ' .. table.concat(response_body))
end
end
The server throws an error when using this function. The response is not helpful as it seems like an internal server error.
I am guessing it has something to do with the payload, as the curl opposite of this function works fine:
Code:
curl -X PATCH "https://{IP}}/v1/devices/{ID}" \
-H "Authorization: Bearer BEARER_TOKEN" \
-H "Content-Type: application/json" \
-d '[{"attributes":{"isOn":true}}]' \
Is the payload formatted correctly as in the curl?