Difference between revisions of "Function cgiGet()"

From wiki.netio-products.com
Jump to navigation Jump to search
Line 2: Line 2:
 
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()|devices.system.CustomCGI()]]
 
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()|devices.system.CustomCGI()]]
  
  <i>nil</i> devices.system.cgiGet{<u>config</u>}
+
  <i>nil</i> <b>devices.system.cgiGet</b>{<code>config</code>}
  
 
=== Parameters ===
 
=== Parameters ===
  <i>table</i> <u>config</u>
+
  <i>table</i> <tt>config</tt>
* <i>string</i> <u>url</u> address to call
+
* <i>string</i> <code>url</code> address to call
* <i>function{response}</i> <u>callback</u> this function is called when response arrives
+
* <i>function{response}</i> <code>callback</code> this function is called when response arrives
* optional <i>number</i> <u>timeout</u> maximum seconds to wait for response (default 30)
+
* optional <i>number</i> <code>timeout</code> maximum seconds to wait for response (default 30)
* optional <i>number</i> <u>bufferSize</u> bytes of memory to allocate for response (min. 4k, max 1M)
+
* optional <i>number</i> <code>bufferSize</code> bytes of memory to allocate for response (min. 4k, max 1M)
  
 
=== Callback Parameters ===
 
=== Callback Parameters ===
  <i>table</i> <u>response</u>
+
  <i>table</i> <tt>response</tt>
* <i>number</i> <u>result</u> error code of the response (see below)
+
* <i>number</i> <code>result</code> error code of the response (see below)
* <i>string</i> <u>errInfo</u> error description (if result>0)
+
* <i>string</i> <code>errInfo</code> error description (if result>0)
* <i>number</i> <u>received</u> response length
+
* <i>number</i> <code>received</code> response length
* <i>string</i> <u>buffer</u> response body
+
* <i>string</i> <code>buffer</code> response body
  
 
=== Error Code of the response ===
 
=== Error Code of the response ===

Revision as of 19:46, 18 April 2017

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{config}

Parameters

table config
  • 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
  • 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",getResponse}

See also