![]() |
|
Http request - Printable Version +- LogicMachine 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 request (/showthread.php?tid=5792) |
Http request - FatMax - 15.12.2024 I am struggling a litte bit with understanding what is going wrong here: 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
endThe 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? RE: Http request - admin - 16.12.2024 You are missing Content-Length header: Code: ["Content-Length"] = #payload,RE: Http request - FatMax - 16.12.2024 That did the trick, thank you! |