Here's an example from my current project - an API call to a backend for retrieving one of two lists:
function avGetList(mode as integer)
q as string
r as string
http = CreateHTTPConnection()
SetHTTPHost(http, ip.adressAV, 1)
SetHTTPTimeout(http, 5000)
select mode
case 0: // get fetch file
q = "list?Get=fetch"
endCase
case 1: // get os version file
q = "list?Get=os"
endCase
endSelect
SendHTTPRequestASync(http, q)
while GetHTTPResponseReady(http) = 0
// get something else in here
Print( "Connecting..." )
Sync()
endWhile
if GetHTTPResponseReady(http) = -1
Print("Connection failed!")
httpOK = false
r = ""
else
r = GetHTTPResponse(http)
httpOK = true
endif
CloseHTTPConnection(http)
DeleteHTTPConnection(http)
endFunction r
I send the function the mode (0 or 1) that determines what list I want to retrieve from the server. The IP address of the server is externalized into a file rather than hardcoded. I got another function to read that file, and transfer the IP address into the variable ip.addressAV. In actuality, I use a microservices architecture for the backend, so I got several ip addresses, hence it is stored in a type. This makes it easy to target testing servers or production servers without having to recompile and redeploy. But details. Anyhow, I also got a global called httpOK into which I store whether or not the connection is good. This is for later to ensure the app do not crap out on me should for some reason the backend not be reachable. Error checking is what separates a seasoned programmer from a gambler
Later on, whatever is returned from the previous function, is fed into this one (the 's as string' argument):
function placeAvList(mt ref as txtProp_t, s as string)
clearText(txt.main, txt.main + mt.txtItems)
setFontProperties(255, 255, 255, media.font, mt.size)
if httpOK
createText(txt.main, s)
textDraw(txt.main, mt.startX, mt.startY)
else
createText(txt.main, "No connection to server!")
textDraw(txt.main + mt.txtItems, mt.startX, mt.startY)
endif
mt.active = true
mt.txtItems = 0
endFunction
As you can see, here the error checking happens for if the transfer went through ok. If httpOK is true, it'll print the string fed into the function. If not, it'll display an error message.
Sending data to a backend is much the same process. Just add it to the payload of the SendHTTPRequestASync() method and have the server respond with an ok or error and act on that.
true and false is by the way set earlier as predefined global constants (using #constant) of 1 and 0 respectively. AppGameKit do not have boolean as a standard type, so have to cheat using an integer instead...