Quick Apps — net.HTTPClient
Class for handling HTTP/HTTPS queries.
HTTPClient methods
net.HTTPClient(options)
HTTPClient class constructor.
Parameters
options- (optional) The argument oftabletype with options for connection. The only option that can be set is atimeoutexpressed 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 thestringtype with the address to which the query is to be executed. e.g.http://127.0.0.1/api/users,https://google.comparams- (optional) the argument of thetabletype. 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- Abooleanfield that says whether to verify the certificate (defaulttrue). This field is only used for HTTPS connectionmethod- a field of the typestringwhich contains the name of the http method (e.g.GET)data- a field of typestringthat contains the query bodyheaders- a field of typetable, which contains query headers
success(response)- The function will be triggered if the data is sent correctly. Thedataargument passed on is of thetabletype with the following structure:data- a field of typestringcontaining the response bodystatus- a field of typenumbercontaining a response code e.g. 200, 404 etc.headers- a field of typetablecontaining response headers
error(message)- The function will be triggered in case of an error when sending data, e.g. disconnection. The parametermessage(typestring) 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