Reading request.query parameter names

Hello,

Is there a way I can read the actual names of the parameters in the req.query string? I would like to be able to read the number of parameters in the query ( got this - req.query.len() ), and then read the names of the parameters. That way I can use url: https://agent.electricimp.com/myimpid/1234?ch1=0&ch12=0&ch35=0, and find that the first parameter name is “ch1”, the second is “ch12”, and the third is “ch35”. Here is the code I have now:

`outBlob <- blob(12);

http.onrequest(function(req, res) {
res.send(200, “DMX Has been Sent”);
if (req.path == “/1234”) {
server.log(“good pw”);
local y = req.query.len();
server.log(y);
for (local i=0; i<y; i++) {
local val = req.query[“ch”+(i+1)];
local v = val.tofloat();
outBlob[i] = v;
}
}
else {
server.log(“wrong pw”);
}

device.send(“newDMX”, outBlob);
})`

I would like to do something like this:

`outBlob <- blob(12);

http.onrequest(function(req, res) {
res.send(200, “DMX Has been Sent”);
if (req.path == “/1234”) {
server.log(“good pw”);
local y = req.query.len();
server.log(y);
for (local i=0; i<y; i++) {
local h = req.query.parametername[i+1];
local val = req.query.h;
local v = val.tofloat();
local g = [h-“ch”];
outBlob[g] = v;
}
}
else {
server.log(“wrong pw”);
}

device.send(“newDMX”, outBlob);
})`

This way I can only send the channels I want changed, instead of all of them, every request. I know that " local g = [h-“ch”];" makes no sense, I eventually need to strip away the “ch” and get the actual channel number so I know where to write in the blob. I can’t find anything online relating to reading the names of the parameters…

Thanks!!

You can iterate through a table, yes.

eg:

foreach(i,v in req.query) { server.log("parameter="+i); server.log("value="+v); }

Hugo, your a boss!! Thanks!! How can I strip away the “ch” from the parameter name? That way I can write outBlob[channel] = v? I found split() but don’t know if that is correct…

foreach(i,v in req.query) { local val = req.query[i]; server.log(i + "=" + v); local v = val.tofloat(); local channel = [i-"ch"]; server.log(channel); outBlob[channel] = v; }

You actually want to use the slice() method:

local channel = i.slice(2);

Here’s what the Squirrel documentation says about slice:

slice(start,[end])

returns a section of the string as new string. Copies from start to the end (not included). If start is negative the index is calculated as length + start, if end is negative the index is calculated as length + end. If end is omitted end is equal to the string length.</quote:

Perfect! Thank you!!!

You probably also want to convert the channel to an integer, otherwise your array would be indexed by a string. ie:
local channel = i.slice(2).tointeger()
channel[1] is not the same as channel[“1”]

Answered my question before I even asked!! Thanks. That’s why I had to use .tofloat(). I was trying to write strings to the blob…

Is there a function to see if a parameter name (ie. “pw”) exists anywhere in the req.query? The agent doesn’t process the parameters in order for some reason…

You should be able to do this:

if ("pw" in req.query) { //do a thing }

Duh… I wasn’t thinking… Thanks!