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

http request - LUA vs CURL


Question

Posted

Hello everybody. I had issue with sending http request via lua to API. 

Example CURL - RESPONSE 204.
curl -vvv -X DELETE \
  "http://XXX.XXX.XXX.XXX/api/events/history?shrink=1&objectType=device" \
  -H "accept: */*" \
  -H "X-Fibaro-Version: 2" \
  -H "Accept-Language: en" \
  -H "Authorization: Basic XXXX4Z2E0LW1lcWJ5bS12eWZ3eXIXXXX="
-----------------------

EXAPLE OF SCENE


local http = net.HTTPClient()
local baseUrl = "http://XXX.XXX.XXX.XXX/api/events/history"
local queryParams = "?shrink=1&objectType=device"

local headers = {
    ["accept"] = "*/*",
    ["X-Fibaro-Version"] = "2",
    ["Accept-language"] = "en",
    ["Authorization"] = "Basic XXXX4Z2E0LW1lcWJ5bS12eWZ3eXIXXXX="
}

-- Odeslání DELETE požadavku
http:request(baseUrl .. queryParams, {
    options = {
        method = "DELETE",
        headers = headers
    },
    success = function(response)
        fibaro.debug(" HTTP DELETE Success: " .. response.status)
        fibaro.debug("📩 Response: " .. response.data)
    end,
    error = function(err)
        fibaro.debug(" HTTP DELETE Error: " .. err)
    end
})--------------------------------------

RESPONSE 400: WRONG QUERY PARAMETERS

 

 

Anybody can help me? I have no ideas yet. 

 

2 answers to this question

Recommended Posts

  • 0
Posted
30 minutes ago, petrmikes said:

Hello everybody. I had issue with sending http request via lua to API. 

Example CURL - RESPONSE 204.
curl -vvv -X DELETE \
  "http://XXX.XXX.XXX.XXX/api/events/history?shrink=1&objectType=device" \
  -H "accept: */*" \
  -H "X-Fibaro-Version: 2" \
  -H "Accept-Language: en" \
  -H "Authorization: Basic XXXX4Z2E0LW1lcWJ5bS12eWZ3eXIXXXX="
-----------------------

EXAPLE OF SCENE


local http = net.HTTPClient()
local baseUrl = "http://XXX.XXX.XXX.XXX/api/events/history"
local queryParams = "?shrink=1&objectType=device"

local headers = {
    ["accept"] = "*/*",
    ["X-Fibaro-Version"] = "2",
    ["Accept-language"] = "en",
    ["Authorization"] = "Basic XXXX4Z2E0LW1lcWJ5bS12eWZ3eXIXXXX="
}

-- Odeslání DELETE požadavku
http:request(baseUrl .. queryParams, {
    options = {
        method = "DELETE",
        headers = headers
    },
    success = function(response)
        fibaro.debug(" HTTP DELETE Success: " .. response.status)
        fibaro.debug("📩 Response: " .. response.data)
    end,
    error = function(err)
        fibaro.debug(" HTTP DELETE Error: " .. err)
    end
})--------------------------------------

RESPONSE 400: WRONG QUERY PARAMETERS

 

 

Anybody can help me? I have no ideas yet. 

 

urlencode the queryParams ?

  • 0
  • Inquirer
  • Posted

    encoding params not necessary.

     

    Solved:

     

    function QuickApp:deleteEventHistory()
    local url = "http://XXX.XXX.XXX.XXX/api/events/history?shrink=1&objectType=device"
     
    local headers = {
    ["Accept"] = "*/*",
    ["X-Fibaro-Version"] = "2",
    ["Accept-Language"] = "en",
    ["Authorization"] = "Basic XXXXXX4Z2E0LW1lcWJ5bS1XXXXXXXX",
    ["User-Agent"] = "curl/8.7.1",
    ["Connection"] = "close",
    ["Cache-Control"] = "no-cache"
    }
     
    local http = net.HTTPClient()
     
    self:debug("🔹 Sending DELETE request via net.HTTPClient to: " .. url)
     
    http:request(url, {
    options = {
    method = "DELETE",
    headers = headers,
    data = nil
    },
    success = function(response)
    self:debug(" HTTP DELETE Success: " .. response.status)
    self:debug("📩 Response Body: " .. (response.data or "<empty>"))
    end,
    error = function(err)
    self:debug(" HTTP DELETE Error: " .. err)
    end
    })
    end
     
     
    ------------------
     
    ["Connection"] = "close",
    and
    data = nil 
     
    solved an issue
     

    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...