posting data

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

posting data

Post 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?
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: posting data

Post by HvdW »

What about filling in a domain?
What do you see when you open https://domain.tld/script in a browser?
Bugs bug me.
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post 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.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: posting data

Post by waltervl »

What if you replace the switch attributes with text to test?

Code: Select all

            postData = {
                idx = '25',
                status = 'On',
                level = '10'
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post by vespino »

Same, I tested with the examples found on the wiki page.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: posting data

Post 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"
    },
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post 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.
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: posting data

Post 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()
Bugs bug me.
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post by vespino »

@hvdw I already tested it like that just to make sure it’s not the data missing that is causing the issue.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: posting data

Post 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).
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post 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.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: posting data

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
habahabahaba
Posts: 190
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: posting data

Post 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.
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post 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)
Last edited by vespino on Thursday 19 December 2024 18:12, edited 1 time in total.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: posting data

Post by jvdz »

I would first try to put the data in a quoted string.
Data = "{.....}"
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post 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)
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: posting data

Post by jvdz »

My only suggestion was to use a proper string instead of lua table not to change post to get!
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
habahabahaba
Posts: 190
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: posting data

Post 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)
            }
vespino
Posts: 48
Joined: Tuesday 05 August 2014 14:41
Target OS: Linux
Domoticz version:

Re: posting data

Post 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.
User avatar
habahabahaba
Posts: 190
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: posting data

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest