Page 1 of 2

posting data

Posted: Wednesday 18 December 2024 15:06
by vespino
I have the following:

Code: Select all

return {
    on = {
        devices = {
            'Wekker',
        }
    },
    execute = function(domoticz, switch)
        domoticz.openURL({
            url = 'https://domain.tld/script',
            method = 'POST',
            postData = {
                idx = switch.idx,
                status = switch.state,
                level = switch.level
            }
        })
    end
}
My problem is the data isn't POST. I have tried posting the data to my test script using cURL, that does work.

Any tips on how to get this working?

Re: posting data

Posted: Wednesday 18 December 2024 17:23
by HvdW
What about filling in a domain?
What do you see when you open https://domain.tld/script in a browser?

Re: posting data

Posted: Wednesday 18 December 2024 18:25
by vespino
The script works fine when calling it in the browser. Also Domoticz triggers the script, but the POST values just are empty/absent.

Re: posting data

Posted: Wednesday 18 December 2024 18:36
by waltervl
What if you replace the switch attributes with text to test?

Code: Select all

            postData = {
                idx = '25',
                status = 'On',
                level = '10'

Re: posting data

Posted: Wednesday 18 December 2024 19:26
by vespino
Same, I tested with the examples found on the wiki page.

Re: posting data

Posted: Wednesday 18 December 2024 19:39
by waltervl
Did you use the curl test from the Domoticz server? Perhaps the server you want to reach is blocked from the Domoticz server?

And what if you set log level to debug?
Add

Code: Select all

logging = {
    level = domoticz.LOG_DEBUG,
    marker = "POST Test"
    },

Re: posting data

Posted: Wednesday 18 December 2024 19:55
by vespino
It isn’t blocked. The script is supposed to create a file and add the POST data to that file. The file is created, however the array is empty.

Re: posting data

Posted: Wednesday 18 December 2024 22:02
by HvdW
Test with predefined data
postData = { idx = '25', status = 'On', level = '10' }
vespino wrote: Wednesday 18 December 2024 19:55 The script is supposed to create a file and add the POST data to that file. The file is created, however the array is empty.

Code: Select all

 
 	local yourFile = "/home/pi/nameOfFile.csv"       
 	
 	local file = io.open(yourFile, "a")
        file:write(currentDate, ";", moredata,  "\n")
        file:close()

Re: posting data

Posted: Wednesday 18 December 2024 22:24
by vespino
@hvdw I already tested it like that just to make sure it’s not the data missing that is causing the issue.

Re: posting data

Posted: Wednesday 18 December 2024 22:28
by waltervl
If curl from the Domoticz server work, in theory openUrl() should also work. So please do the curl c from the Domoticz server (you did not explicitly answer my question about this).

Re: posting data

Posted: Wednesday 18 December 2024 22:41
by vespino
I’m running Domoticz on Docker, logged into the command line and ran the following curl command:

Code: Select all

curl -d 'id=9&name=baeldung' https://domain.tld/script
This created file with an array/values that I posted.

Re: posting data

Posted: Wednesday 18 December 2024 23:46
by waltervl
With the debug on you probably can see in the log what is sent as POST, see for example viewtopic.php?p=317025&hilit=Openurl+Post#p317025

Re: posting data

Posted: Thursday 19 December 2024 17:34
by habahabahaba
Wiki

Posting data should be a string.

Is it?

My code is:

Code: Select all

domoticz.openURL({
                url = APIURL,
                method = 'POST',
                postData = { msgtoAlice = postString }
            }).afterSec(1)
It works but i have only 1 param to post.

Re: posting data

Posted: Thursday 19 December 2024 18:09
by vespino
Data seems to be post:

Code: Select all

2024-12-19 18:06:12.217 dzVents: Debug: POST Test: OpenURL: method = POST
2024-12-19 18:06:12.217 dzVents: Debug: POST Test: OpenURL: post data = {"idx":108,"level":15,"status":"On"}
2024-12-19 18:06:12.217 dzVents: Debug: POST Test: OpenURL: headers = !#Content-Type: application/json
2024-12-19 18:06:12.217 dzVents: Debug: POST Test: OpenURL: callback = nil
Data received is still empty though. Could it be the JSON header (that I now notice)?

(This is probably just a stupid thing, so sorry in advance)

Re: posting data

Posted: Thursday 19 December 2024 18:12
by jvdz
I would first try to put the data in a quoted string.
Data = "{.....}"

Re: posting data

Posted: Thursday 19 December 2024 18:30
by vespino
@jvdz that does do something, but the reason I prefer POST over GET is the code is better readable and this way I can use GET just as well (first world problem)

Re: posting data

Posted: Thursday 19 December 2024 19:30
by jvdz
My only suggestion was to use a proper string instead of lua table not to change post to get!

Re: posting data

Posted: Thursday 19 December 2024 19:39
by habahabahaba
My opinion:

Code: Select all

{"idx":108,"level":15,"status":"On"}
- almost the correct json format except 108 and 15

So every posting param must be a string
Try

Code: Select all

postData = {
                idx = tostring(switch.idx),
                status = switch.state,
                level = tostring(switch.level)
            }

Re: posting data

Posted: Thursday 19 December 2024 20:13
by vespino
@jvdz I didn’t change post to get, I was referring to the markup. I was hoping to use post so the variables are more clearly visible instead of one long string as with get.

@habahabahaba adding tostring doesn’t make a difference.

Re: posting data

Posted: Friday 20 December 2024 7:04
by habahabahaba
Made some test with Node-red.

1.
Out:

Code: Select all

postData = {
                                idx = 101,
                                status = Off,
                                level = 15,
                                param1 = qweqwe,
                                qwer = werewqr,
                                open = true
                            }
In:

Code: Select all

{"idx":101,"level":15,"open":true}
Lost some data

2.
Out:

Code: Select all

postData = {
                                idx = '101',
                                status = On,
                                level = '15',
                                param1 = 'qweqwe',
                                qwer = 'werewqr',
                                open = 'true'
                            }
In:

Code: Select all

{"idx":"101","level":"15","open":"true","param1":"qweqwe","qwer":"werewqr"}
Lost 1 param

3.
Out:

Code: Select all

postData = {
                                idx = tostring(btn.idx),
                                status = 'On',
                                level = '15',
                                param1 = 'qweqwe',
                                qwer = 'werewqr',
                                open = 'true'
                            }
In:

Code: Select all

{"idx":"101","level":"15","open":"true","param1":"qweqwe","qwer":"werewqr","status":"On"}
OK

4.
Out:

Code: Select all

postData = {
                                idx = tostring(btn.idx),
                                status = btn.state,
                                level = '15',
                                param1 = 'qweqwe',
                                qwer = 'werewqr',
                                open = 'true'
                            }
In:

Code: Select all

{"idx":"101","level":"15","open":"true","param1":"qweqwe","qwer":"werewqr","status":"On"}
OK

So.
Every param have to be a string value and everything works fine.

PS. The params in result JSON string automatically sorted in alphabetical order

PPS And dont add any headers params in domoticz.openUrl - it gives wrong output json format