Logic Machine Forum
http post/request - 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/request (/showthread.php?tid=4913)



http post/request - Dré - 08.08.2023

I'm trying to sent a command to a homewizzard socket, to put him on and off, but really don't know how to sent the command.

I can read the value's but don;t know to sent it on and off.

hope someone can help me?

this is what i use to read
Code:
require('socket.http')

ip = '192.168.99.73'         --Ip adres of the socket

socket.http.TIMEOUT = 5
data = socket.http.request('http://'..ip..'/api/v1/state')

--{"power_on":true,"switch_lock":true,"brightness":0}
if data then
log('HW socket: Televisie: State'..data)
        end

this is what the site said about the command.
Code:
Turn on socket

<Request>
PUT http://{IP address}/api/v1/state HTTP/1.1
Content-Type: application/json
Content-Length: <length>
{
   "power_on": true
}

<Response>
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: <length>

{[url=https://homewizard-energy-api.readthedocs.io/endpoints.html]page of HomeWizzard[/url]
   "power_on": true
}


what i get when read the url:
P1 HomeWizzard TEST 08.08.2023 12:57:02
* string: HW socket: Televisie: State{"power_on":true,"switch_lock":false,"brightness":0}


RE: http post/request - admin - 08.08.2023

Try this:
Code:
json = require('json')
http = require('socket.http')

body = json.encode({
  power_on = true
})

resp, code, hdrs, stat = http.request({
  url = 'http://{IP address}/api/v1/state',
  method = 'PUT',
  headers = {
    ['Content-Type'] = 'application/json',
    ['Content-Length'] = #body,
  },
  body = body
})

log(resp, code, hdrs, stat)



RE: http post/request - Dré - 08.08.2023

Thanks a lot, this is working.