Quick Apps — net.HTTPClient | FIBARO Dev

Quick Apps — net.HTTPClient

Class for handling HTTP/HTTPS queries.

HTTPClient methods

net.HTTPClient(options)

HTTPClient class constructor.

Parameters

  • options - (optional) The argument of table type with options for connection. The only option that can be set is a timeout expressed in milliseconds.

Example

1function QuickApp:onInit()
2    self.http = net.HTTPClient({timeout=3000})
3end

HTTPClient:request(address, params)

A method to execute HTTP/HTTPS queries.

Parameters

  • address - the argument of the string type with the address to which the query is to be executed. e.g. http://127.0.0.1/api/users, https://google.com
  • params - (optional) the argument of the table type. The argument may contain options for the query and/or feedback functions. The possible parameters are described below

items of the params array

  • options - an array of query options. Possible options:
    • checkCertificate - A boolean field that says whether to verify the certificate (default true). This field is only used for HTTPS connection
    • method - a field of the type string which contains the name of the http method (e.g. GET)
    • data - a field of type string that contains the query body
    • headers - a field of type table, which contains query headers
  • success(response) - The function will be triggered if the data is sent correctly. The data argument passed on is of the table type with the following structure:
    • data - a field of type string containing the response body
    • status - a field of type number containing a response code e.g. 200, 404 etc.
    • headers - a field of type table containing response headers
  • error(message) - The function will be triggered in case of an error when sending data, e.g. disconnection. The parameter message (type string) returns the error content.

Examples

1-- An example of a GET inquiry 
2-- self.http must have been previously created by net.HTTPClient
3function QuickApp:getQuote()
4    local address = "https://quotes.rest/qod"
5
6    self.http:request(address, {
7        options={
8            headers = { 
9                Accept = "application/json"
10            },
11            checkCertificate = true,
12            method = 'GET'
13        },
14        success = function(response)
15            self:debug("response status:", response.status) 
16            self:debug("headers:", response.headers["Content-Type"]) 
17            local data = json.decode(response.data)  
18            if data.contents and data.contents.quotes and data.contents.quotes[1] then
19                local quote = data.contents.quotes[1].quote
20                self:debug(quote)
21                self:updateView("label", "text", quote) 
22            end
23        end,
24        error = function(error)
25            self:debug('error: ' .. json.encode(error))
26        end
27    }) 
28end
1-- An example of a POST inquiry  
2-- self.http must have been previously created by net.HTTPClient
3function QuickApp:createBackup(description)
4    local requestBody = {
5        action = "create",
6        params = {
7            type = "local",
8            description = description
9        }
10    }
11
12    self.http:request("http://127.0.0.1/api/service/backups", {
13        options = {
14            data = json.encode(requestBody),
15            method = "POST",
16            headers = {
17                ["Content-Type"] = "application/json",
18                ["Accept"] = "application/json",
19                ["Authorization"] = "Basic  YWRtaW46YWRtaW4=",
20            }
21        },
22        success = function(response) 
23            self:debug(response.status)
24            self:debug(response.data)
25        end,
26        error = function(message)
27            self:debug("error:", message)
28        end
29    })
30end