LogicMachine Forum
Remote control of Philips TV with json - 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: Remote control of Philips TV with json (/showthread.php?tid=527)



Remote control of Philips TV with json - baggins - 29.12.2016

Hi,

I'm trying to write a script to control a Philips TV with json.
Based on an example in thread: http://forum.logicmachine.net/showthread.php?tid=509&highlight=POST

I'm trying the following:


Code:
require('json') require('socket.url') require('socket.http') fields = {key = "CursorLeft",} data = json.encode(fields) print(data) post = 'data=' .. socket.url.escape(data) print (post) ip = '192.168.0.232:1925' res, err = socket.http.request('http://'.. ip .. '/1/input/key', post) print(res) print( err)

This fails:


Code:
{"key":"CursorLeft"} data=%7b%22key%22%3a%22CursorLeft%22%7d <html><head><title>Bad Request</title></head><body>Bad Request</body></html> 400


Philips TV use Jointspace. Manual page for POST: http://jointspace.sourceforge.net/projectdata/documentation/jasonApi/1/doc/API-Method-input-key-POST.html

Any  tips?


RE: Remote control of Philips TV with json - admin - 30.12.2016

Can you check if any GET methods are working?


RE: Remote control of Philips TV with json - baggins - 30.12.2016

After a long session with Wireshark I gave up on this script...

I now use the following script which works:


Code:
require('json') require('socket.url') require('socket.http') local ltn12 = require("ltn12") path = "http://192.168.0.232:1925/1/input/key" local payload = [[{"key" : "CursorLeft"}]] print(payload) local response_body = { } local res, code, response_headers, status = socket.http.request  {    url = path,    method = "POST",    headers =    {          ["Content-Type"] = "application/json",          ["Content-Length"] = payload:len()    },   source = ltn12.source.string(payload),    sink = ltn12.sink.table(response_body)  } print('Response: = ' .. table.concat(response_body) .. ' code = ' .. code .. '   status = ' .. status,1,'Sample POST request with JSON data',-1)



RE: Remote control of Philips TV with json - admin - 30.12.2016

Probably it didn't work without proper Content-Type header.