This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

curl to lua
#1
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.
Reply
#2
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)
Reply
#3
(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!
Reply
#4
Specifying sink is not needed in newer firmwares. The library will create a sink automatically and will return the request result as a string.
Reply
#5
(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...
Reply


Forum Jump: