postRequest(url:byte[], body:byte[])->bool
For this function, the card must be connected to the Internet.
Sends a POST request to the defined URL.
The function returns true if the request was successful (200 OK).
Example:
# Toggle Shelly relay
url = "http://192.168.42.146/relay/0"
body = "turn=toggle"
def onDraw():
if getButton():
postRequest(url, body)
while(getButton()): delay(50)
delay(50)
readRequestJSON(jsonPath:byte[])->byte[128]
Returns the content of a JSON response as text (max. 128 bytes) of the request previously requested with getRequest().
With jsonPath the key to be read can be defined.
With ‘.’ or ‘/’ also nested JSON objects can be selected.
Example1:
url = "http://api.nanopy.io/v1/utc"
getRequest(url) # {"utc":1667912328}
drawText(10, 10, readRequestJSON("utc"))
update()
Example2:
# {
# "myObj": {
# "mySubObj": {
# "myArray": [
# {"myKey":111},
# {"myKey":222},
# {"myKey":333}
# ],
# }
# }
# }
readRequestJSON("myObj.mySubObj.myArray.0.myKey") # "111"
readRequestJSON("myObj/mySubObj/myArray/2/myKey") # "333"
Note:
readRequestJSON() always returns a string (byte[]).
The stringTo…() functions can then be used to convert the result to the appropriate data type.
readRequest(offset:int)->byte[128]
Returns the text (max. 128 bytes) of the request previously requested with getRequest().
With offset the start position can be defined, which determines from where the response should be read.
This allows to read longer responses in several steps.
Example:
url = "http://api.nanopy.io/v1/hello_world"
getRequest(url)
drawText(10, 10, readRequest(0))
drawText(10, 50, readRequest(6))
update()
Note:
If you use your own server and want to return plain text, you must also define the ‘Content-Length’ in the header.
readRequestLength()->int
Returns the length (in bytes) of the request previously requested with getRequest().
getRequest(url:byte[])->bool
For this function, the card must be connected to the Internet.
Sends a GET request to the defined URL.
The function returns true if the request was successful (200 OK).
The requested data can then be read with the following functions:
readRequestLength()
readRequest(offset)
readRequestJSON(jsonPath)
Example:
url = "http://api.nanopy.io/v1/utc"
if (getRequest(url)):
utc = readRequestJSON("utc")
drawText(10, 10, stringToInt(utc))
else:
drawText(10, 10, "error")
update()
Limitations: - A maximum of 8k bytes can be requested. Longer responses will be truncated - https requests are not supported