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.

openDTU - JSON - http post - "no values found"
#1
Hi all,

I'm trying to integrate / control openDTU² into my W4k. I can already read the data from my openDTU. Now I'm trying to write data to my openDTU, which fails. Any idea?

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

body = json.encode({
    limit_value = '10',
    limit_type = '1',
    serial = '000000000000'
})

resp, code, hdrs, stat = http.request({
  url = 'http://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Type'] = 'application/json',
    ['Content-Length'] = #body,
  },
  body
})

log(resp, code, hdrs, stat)

I get the following response in the log:

Code:
* arg: 1
  * nil
* arg: 2
  * string: closed
* arg: 3
  * nil
* arg: 4
  * nil



In case it's done; I'll post the full code here. 

²https://www.opendtu.solar/firmware/web_api/#list-of-urls
Reply
#2
Try this:
Code:
json = require('json')
ltn12 = require('ltn12')
http = require('socket.http')

body = json.encode({
  limit_value = '10',
  limit_type = '1',
  serial = '000000000000'
})

resp, code, hdrs, stat = http.request({
  url = 'http://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Type'] = 'application/json',
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
})

log(resp, code, hdrs, stat)
Reply
#3
This is something which I already tried. In this case I get the following log:

Code:
* arg: 1
  * string: {"type":"warning","message":"No values found!","code":1002}
* arg: 2
  * number: 200
* arg: 3
  * table:
   ["content-length"]
    * string: 59
   ["accept-ranges"]
    * string: none
   ["content-type"]
    * string: application/json
   ["connection"]
    * string: close
* arg: 4
  * string: HTTP/1.1 200 OK

I log "body" as well and get the following result:

 
Code:
* string: {"limit_value":"10","limit_type":"1","serial":"000000000000"}

I don't understand why the serial is still in the thrid position. I changed the code and expect the serial to be on first position. Why does this not happen?
Code:
body = json.encode({   
   serial = '000000000000',
    limit_type = '1',   
    limit_value = '10',
})


For a common understandig - although it's not shown hier in the code, I use the correct serial and IP.
Reply
#4
Try this:
Code:
json = require('json')
ltn12 = require('ltn12')
http = require('socket.http')
url = require('socket.url')

body = json.encode({
  limit_value = '10',
  limit_type = '1',
  serial = '000000000000'
})

body = 'data=' .. url.escape(body)

resp, code, hdrs, stat = http.request({
  url = 'https://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
})

log(resp, code, hdrs, stat)
Reply
#5
This didn't work either. Nevertheless I found a solution for setting the limit without using the json lib. 


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

body = 'data={"serial":"000000000000", "limit_type":"1", "limit_value":"'..limit..'"} '

res, code, headers, status = http.request({
  url = 'http://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Type'] = 'application/x-www-form-urlencoded',
    ['Content-Length'] = #body,
  },
    source = ltn12.source.string(body),
})

log(res, code, headers, status)
Reply


Forum Jump: