Sending several string arrays through http.onrequest()

I have the following code on the Agent side:

// Define an HTTP request handler
function requestHandler(request, response) {
try {
if (“limit” in request.query) {
// ‘limit’ is a URL-encoded parameter, ie. ‘?limit=4’
local uuid, minor, major, power, totalBeacons = null
local settingValue = request.query.limit.tointeger();
local starterFlag = “We have just started!”
device.send(“begin”, starterFlag)
device.on(“retrieve”, function(myBeacon) {
//Make sure we turned on the device:
server.log(“device on”);
totalBeacons = myBeacon.len();
if (settingValue > totalBeacons) {

                response.send(200, ("Amount of Beacons is: " + totalBeacons + ", which is lower than the amount of beacons requested (" +settingValue+")" ))
                for (local i = 0 ; i < totalBeacons ; i++) {
                    uuid = myBeacon[i].slice(0, 32);
                    major = myBeacon[i].slice(33,34);
                    minor = myBeacon[i].slice(35,36);
                    power = myBeacon[i].slice(37,40);
                    server.log(("This is beacon ID number "+(1+i)+":"))
                    server.log(("UUID is: " + uuid))
                    server.log(("Major is: " + major))
                    server.log(("Minor is: " + minor))
                    server.log(("Power is: " + power))
                    server.log(("End of description of beacon ID number "+(1+i)+":"))
                    
                }
            }
            if (settingValue <= totalBeacons) {
                if (settingValue ==0){
                    response.send(200, ("Please change the amount of beacons requested in the query, currently is " + settingValue + " (zero)" ))
                } else if (settingValue!=0){
                    response.send(200, ("Amount of Beacons is: " + totalBeacons + ", the amount of beacons requested is " +settingValue+", will only print as requested" ))
                    for (local i = 0 ; i < settingValue ; i++) {
                        uuid = myBeacon[i].slice(0, 32);
                        major = myBeacon[i].slice(33,34);
                        minor = myBeacon[i].slice(35,36);
                        power = myBeacon[i].slice(37,40);
                        server.log(("This is beacon ID number "+(1+i)+":"))
                        server.log(("UUID is: " + uuid))
                        server.log(("Major is: " + major))
                        server.log(("Minor is: " + minor))
                        server.log(("Power is: " + power))
                        server.log(("End of description of beacon ID number "+(1+i)))
                    }
                    
                }
                
            }
            
        });
        
        
        
                
        // Use the 'response' object to acknowledge reception of the request
        // to the request's source. '200' is HTTP status code for 'OK'
        
        
    }
} catch (error) {
    // Something went wrong; inform the source of the request
    // '500' is HTTP status code for 'Internal Server Error'
    response.send(500, error); 
}

}

// Register the handler function as a callback
http.onrequest(requestHandler);

This gets executed when I run the following URL: https://agent.electricimp.com/[API KEY]/closestIBeacons?limit=1 (limit can be set to any integer I like). The outcome is as the log shows:

image

I would like to upload this data (the info I’m printing with server.log()) to the URL from which I’m initiating my application.

Is there any way how? I’d like to know please.

Thanks in advance!

I remembered I saw htttp.jsonencoder() and used it to send the data to the web API:

I noticed you could compart them using keys. Is there a way to do this within the loop?
Also, is there a way to do a nested table {} with keys and then append/push them inside the loop and at the very end, use http.jsonencoder()?

this approach worked (althought not on the order expected).

First I initiated a new table as global. Having an array as global as well.

printBeacons <-
tableBeacons ← {}

Then I do this in the conditionals:

tableBeacons.uuid ← myBeacon[i].slice(0, 32);
tableBeacons.major ← myBeacon[i].slice(33,34);
tableBeacons.minor ← myBeacon[i].slice(35,36);
tableBeacons.power ← myBeacon[i].slice(37,40);
printBeacons.push(tableBeacons)

And outside of the conditionals I just do this:

printBeacons = http.jsonencode(printBeacons);
response.send(200, printBeacons)

This worked but if you have a better approach I’d be happy to hear :slight_smile:

Thanks in advance!