Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

is it possible to do http post in LUA Scene?


Question

Posted (edited)

Hi,

 

I'm trying to use net.HTTPClient() to do a post request in HC3. It works in QuickApp, but not in LUA Scene.

Is there any way I could do a http post in LUA Scenes?

Thank you.

 

Edited by fuser99

18 answers to this question

Recommended Posts

  • 0
Posted

Yes, see the Alexa

Please login or register to see this link.

. Check the options in the header:

Please login or register to see this code.

 

  • 0
  • Inquirer
  • Posted
    22 minutes ago, Joep said:

    Yes, see the Alexa

    Please login or register to see this link.

    . Check the options in the header:

    Please login or register to see this code.

     

    Thank you. It works on QuickApp, but it doesn't work on LUA Scene. I get  200 status respone but doesn't work.

    What I actually want is to do a request to 

    http://10.254.5.254/webhook?event=e1&tag=t1

    But it seems it does a request to http://10.254.2.254/webhook instead of the full url  (http://10.254.5.254/webhook?event=e1&tag=t1)

    I even tried with GET and still the same issue, what can I do?

    If I do it via QuickApp the same way it works, but it doesn't work in LUA Scene.

     
     
     
     
    • 0
    Posted

    This is a GET request with the parameters into the link. This is not a POST request.

    Yes, get request with application/x-www-form-urlencoded data (data in the url) is not supported in the HTTP:request implementationin a scene. I found that out the hard way.
    It's stupid, but the HTTP implementation of the scene and Quick App parts is different. Why FIBARO?

    • 0
  • Inquirer
  • Posted

    Is there a way to do what I need?

    Thank you.

    • 0
    Posted

    You're example is not working in a Lua scene. Write a QA that does it and let the scene trigger the QA as a workaround.
    It's not nice, I know.

    • 0
    Posted
    1 hour ago, fuser99 said:

    Is there a way to do what I need?

    Thank you.

    Can you show us the full request code that works in a QA?

    You can encode parameters in the URL of a POST...

    I guess you don't have a body for the POST?

    ...I believe that there is something else going on here

    • 0
    Posted
    3 minutes ago, jgab said:

    ...I believe that there is something else going on here

    Yes, he’s doing a GET request with the parameters in the URL.

    • 0
  • Inquirer
  • Posted
    3 hours ago, jgab said:

    Can you show us the full request code that works in a QA?

    You can encode parameters in the URL of a POST...

    I guess you don't have a body for the POST?

    ...I believe that there is something else going on here

     

    Here is the full code that works great with QA:

     

    -- Binary switch type should handle actions turnOn, turnOff
    -- To update binary switch state, update property "value" with boolean
     
    function QuickApp:doTest(url)
    net.HTTPClient():request(url, {
        options = {
            method = 'POST',
            checkCertificate = false,
            timeout = 1000,
            headers= {
                ['Content-Type'] = 'application/json',
                ['accept'] = '*/*'
                }
        },
        success = function(response)
            self:debug(response.status .. " " .. response.data)
        end,
        error = function(message)
            self:debug("HTTPClient error: " .. message)
        end
    })
     
    end
     
    function QuickApp:turnOn()
        self:debug("binary switch turned on")
        QuickApp:doTest("http://10.254.5.254/webhook?event=e1&tag=t2")
        self:updateProperty("value"true)
    end
     
    function QuickApp:turnOff()
        self:debug("binary switch turned off")
        QuickApp:doTest("http://10.254.5.254/webhook?event=e2&tag=t2")
        self:updateProperty("value"false)    
    end
     
    -- To update controls you can use method self:updateView(<component ID>, <component property>, <desired value>). Eg:  
    -- self:updateView("slider", "value", "55") 
    -- self:updateView("button1", "text", "MUTE") 
    -- self:updateView("label", "text", "TURNED ON") 
     
    -- This is QuickApp inital method. It is called right after your QuickApp starts (after each save or on gateway startup). 
    -- Here you can set some default values, setup http connection or get QuickApp variables.
    -- To learn more, please visit: 
    --    * https://manuals.fibaro.com/home-center-3/
    --    * https://manuals.fibaro.com/home-center-3-quick-apps/
     
    function QuickApp:onInit()
        self:debug("onInit")
    end
     
    2 hours ago, Joep said:

    Yes, he’s doing a GET request with the parameters in the URL.

     

    also QA works with "GET" as method.

    • 0
    Posted

    Ah, you are doing a POST. Normally you have a data payload with the parameters then.

     

    What I said. Parameters in the URL don't work in a HTTP scene. I don't know why.

    I tested this with an own HTTP endpoint to see what the HC3 sends, and from a scene the parameters are gone and with a QA they are still there.

     

    • 0
  • Inquirer
  • Posted
    1 hour ago, Joep said:

    Ah, you are doing a POST. Normally you have a data payload with the parameters then.

     

    What I said. Parameters in the URL don't work in a HTTP scene. I don't know why.

    I tested this with an own HTTP endpoint to see what the HC3 sends, and from a scene the parameters are gone and with a QA they are still there.

     

    I also noticed that. is there a workaround on this?

    • 0
    Posted
    22 minutes ago, fuser99 said:

    I also noticed that. is there a workaround on this?

     

    I answered that question twice. 

    • 0
    Posted
    3 hours ago, Joep said:

    Ah, you are doing a POST. Normally you have a data payload with the parameters then.

     

    What I said. Parameters in the URL don't work in a HTTP scene. I don't know why.

    I tested this with an own HTTP endpoint to see what the HC3 sends, and from a scene the parameters are gone and with a QA they are still there.

     

    If you urlencode the parameters?

    • 0
    Posted
    15 minutes ago, jgab said:

    If you urlencode the parameters?

    For me that didn’t work.

    • 0
  • Inquirer
  • Posted
    10 hours ago, jgab said:

    If you urlencode the parameters?

    Unfortunately, It doesn't work.

    • 0
    Posted (edited)
    10 hours ago, Joep said:

    For me that didn’t work.

    This works in a scene

    Please login or register to see this code.

    if the parameter part is not urlencoded it's stripped away by the HC3/Scene

    Edited by jgab
    • Like 1
    • Thanks 1
    • 0
    Posted
    1 hour ago, jgab said:

    if the parameter part is not urlencoded it's stripped away by the HC3/Scene

     

    Thank you. I tried urlencode via  a website. That didn't work. Good to know it works this way.

    Totally strange a QA doesn't need the urlencode.

    • 0
    Posted
    3 hours ago, Joep said:

    Totally strange a QA doesn't need the urlencode.

     

    different http implementation, it is like that since years, for scene was always necessary to urlencode the param part of url

    • 0
  • Inquirer
  • Posted
    On 3/19/2024 at 8:37 AM, jgab said:

    This works in a scene

    Please login or register to see this code.

    if the parameter part is not urlencoded it's stripped away by the HC3/Scene

     

    Looks like it works this way. Perhaps it didn't work before because I tried to encode the whole url.

    Thank you!

     

     

    • Like 1

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...