Difference between revisions of "Function cgiGet()"
Jump to navigation
Jump to search
(→Parameters) |
|||
(6 intermediate revisions by 3 users not shown) | |||
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 === | ||
− | <i>table</i> < | + | <i>table</i> config{<code>url</code>, <code>callback</code>, <code style="color: #777">timeout</code>, <code style="color: #777">bufferSize</code>, <code style="color: #777">sslVerifyPeer=false</code>} |
* <i>string</i> <code>url</code> address to call | * <i>string</i> <code>url</code> address to call | ||
* <i>function{response}</i> <code>callback</code> 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> <code>timeout</code> 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> <code>bufferSize</code> 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) | ||
+ | * optional <i>number</i> <code>sslVerifyPeer=false</code> use if you don't want to verify peer's certificate | ||
=== Callback Parameters === | === Callback Parameters === | ||
− | <i>table</i> < | + | <i>table</i> response{<code>result</code>, <code>errInfo</code>, <code>received</code>, <code>buffer</code>} |
* <i>number</i> <code>result</code> error code of the response (see below) | * <i>number</i> <code>result</code> error code of the response (see below) | ||
* <i>string</i> <code>errInfo</code> error description (if result>0) | * <i>string</i> <code>errInfo</code> error description (if result>0) | ||
Line 28: | Line 29: | ||
=== 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", callback=logOurIp, timeout=2} | |
+ | |||
+ | === Requirements === | ||
+ | Netio FirmWare 2.3.5+ | ||
=== See also === | === See also === |
Latest revision as of 20:09, 13 August 2020
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
,sslVerifyPeer=false
}
- 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) - optional number
sslVerifyPeer=false
use if you don't want to verify peer's certificate
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
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", callback=logOurIp, timeout=2}
Requirements
Netio FirmWare 2.3.5+
See also
- devices.system.CustomCGI() to send HTTP request without response handling