Logic Machine Forum
curl to lua - 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: curl to lua (/showthread.php?tid=3823)



curl to lua - baggins - 22.01.2022

Hi,

I am trying to convert a curl command to lua but cannot get it to work...
This is the curl command:

Code:
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0", "id":1", "method": "Input.Down"}' \
http://user:passwd@192.168.0.24:8080/jsonrpc
Result:
Code:
{"id":"1", "jsonrpc":"2.0", "result":"OK"}

This is the lua code:

Code:
socket = require('socket.http')

socket.TIMEOUT = 5
url = 'http://user:passwd@192.168.0.24:8080/jsonrpc'
body = '{"jsonrpc": "2.0", "id": 1, "method": "Input.Down"}'

res, code = socket.request({
    url = url,
    method = 'POST',
    body = body,
    headers = {'Content-Type: application/json'
        }
})                       
log(res)
log(code)
This results in:
Code:
testing 22.01.2022 16:14:46
* number: 200
testing 22.01.2022 16:14:46
* number: 1

Any hints?
TIA.


RE: curl to lua - Erwin van der Zwart - 22.01.2022

Try this:
Code:
require('socket.http')
require("ltn12")

res = {}
body = '{"jsonrpc":"2.0","id":1,"method":"Input.Down"}'
socket.http.request({
  url = "http://user:passwd@192.168.0.24:8080/jsonrpc",
  method = 'POST',
  sink = ltn12.sink.table(res),
  headers = {
    ['content-length'] = #body,
    ['content-type'] = 'application/json',
  },
  source = ltn12.source.string(body),
})

log(res)



RE: curl to lua - baggins - 22.01.2022

(22.01.2022, 22:58)Erwin van der Zwart Wrote: Try this:
Code:
require('socket.http')
require("ltn12")

res = {}
body = '{"jsonrpc":"2.0","id":1,"method":"Input.Down"}'
socket.http.request({
  url = "http://user:passwd@192.168.0.24:8080/jsonrpc",
  method = 'POST',
  sink = ltn12.sink.table(res),
  headers = {
    ['content-length'] = #body,
    ['content-type'] = 'application/json',
  },
  source = ltn12.source.string(body),
})

log(res)

This works!

Bedankt Erwin!


RE: curl to lua - admin - 24.01.2022

Specifying sink is not needed in newer firmwares. The library will create a sink automatically and will return the request result as a string.


RE: curl to lua - baggins - 24.01.2022

(24.01.2022, 07:08)admin Wrote: Specifying sink is not needed in newer firmwares. The library will create a sink automatically and will return the request result as a string.

I am on 20160714 firmware...