Difference between revisions of "Function cgiGet()"
Jump to navigation
Jump to search
(→Usage) |
|||
| 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> <b> | + | <i>nil</i> <b>cgiGet</b>(<i>table</i> <code>config</code>) |
=== Parameters === | === Parameters === | ||
| Line 28: | Line 28: | ||
=== Usage === | === Usage === | ||
| − | + | function logOurIp(o) | |
| − | + | if o.result == 0 then | |
| − | if | + | log(string.format("our ip is %s", string.match(o.buffer,"(%d+.%d+.%d+.%d+)"))) |
| − | |||
else | else | ||
| − | + | log(string.format("cgi to get our ip failed with error %d: %s", o.result, o.errorInfo)) | |
end | end | ||
end | end | ||
| − | -- | + | -- send HTTP request and handle response |
| − | + | cgiGet{url="http://httpbin.org/ip", timeout=2, callback=logOurIp} | |
=== Requirements === | === Requirements === | ||
Revision as of 13:54, 11 July 2019
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 cgiGet(table config)
Parameters
table config{url, callback, timeout, bufferSize}
- string
urladdress to call - function{response}
callbackthis function is called when response arrives - optional number
timeoutmaximum seconds to wait for response (default 30) - optional number
bufferSizebytes of memory to allocate for response (min. 4k, max 1M)
Callback Parameters
table response{result, errInfo, received, buffer}
- number
resulterror code of the response (see below) - string
errInfoerror description (if result>0) - number
receivedresponse length - string
bufferresponse 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
function logOurIp(o)
if o.result == 0 then
log(string.format("our ip is %s", string.match(o.buffer,"(%d+.%d+.%d+.%d+)")))
else
log(string.format("cgi to get our ip failed with error %d: %s", o.result, o.errorInfo))
end
end
-- send HTTP request and handle response
cgiGet{url="http://httpbin.org/ip", timeout=2, callback=logOurIp}
Requirements
Netio FirmWare 2.3.5+
See also
- devices.system.CustomCGI() to send HTTP request without response handling