Difference between revisions of "Function cgiGet()"

From wiki.netio-products.com
Jump to navigation Jump to search
(Usage)
Line 29: Line 29:
 
=== Usage ===
 
=== Usage ===
 
  -- callback function to read the response
 
  -- callback function to read the response
  local function getResponse{response}
+
  local function getResponse(response)
 
   if response.result > 0 then
 
   if response.result > 0 then
 
     logf("response failed with error code %d.", response.result)
 
     logf("response failed with error code %d.", response.result)
Line 38: Line 38:
 
   
 
   
 
  -- sends HTTP request and handles the response
 
  -- sends HTTP request and handles the response
  devices.system.CgiGet{url="http://example.com/getcredit?id=42",getResponse}
+
  devices.system.CgiGet{url="http://example.com/getcredit?id=42", callback=getResponse}
  
 
=== Requirements ===
 
=== Requirements ===

Revision as of 12:24, 1 February 2018

Execute HTTP GET call to given url and read the response. The call is asynchronous: the code execution proceeds immediately and when the response arrives, callback function is called. To perform simple HTTP GET call without response handling, use simpler devices.system.CustomCGI()

nil devices.system.cgiGet(table config)

Parameters

table config{url, callback, timeout, bufferSize}
  • string url address to call
  • function{response} callback this function is called when response arrives
  • optional number timeout maximum seconds to wait for response (default 30)
  • optional number bufferSize bytes of memory to allocate for response (min. 4k, max 1M)

Callback Parameters

table response{result, errInfo, received, buffer}
  • number result error code of the response (see below)
  • string errInfo error description (if result>0)
  • number received response length
  • string buffer response body

Error Code of the response

  • 0 success, response HTTP/GET 2xx
  • 22 response HTTP/GET 4xx
  • 27 buffer overflow
  • 28 response is timed out

Return value

nil

Usage

-- callback function to read the response
local function getResponse(response)
  if response.result > 0 then
    logf("response failed with error code %d.", response.result)
  else
    logf("current credit is %d", tonumber(response.buffer))
  end
end

-- sends HTTP request and handles the response
devices.system.CgiGet{url="http://example.com/getcredit?id=42", callback=getResponse}

Requirements

Netio FirmWare 2.3.5+

See also