As I try to server.log and get a better understanding of the request in general, I came across some great documentation: https://electricimp.com/docs/api/httprequest/
“This object is not to be confused with the request table passed into the http.onrequest() callback.”
Is there a way to print out that request table in a human-readable form? I’m just getting hex values right now. I’ve tried decoding it with no luck. Is there anything that better documents all the methods and properties the request object has? I only know there’s a query (request.query) from the example.
Does anyone know what the request actually looks like that these apps send? A JSON object with more than one key-value pair? I only need to a very very simple one-query uri right now.
I’m sure someone had to have solved this since the release of the whole new ide I was just surprised to a couple hours ago…
You can use IoT Buddy with this example, because it does not pass JSON data. Pitchfork and Little Devil both send JSON data that you need to parse out of the body. If you want to use IoT Buddy, you will need to change the example code, because IoT Buddy is going to pass “value=” and not “led=”
`// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?value=1”);
server.log("Turn LED Off: " + http.agenturl() + “?value=0”);
function requestHandler(request, response) {
try {
// check if the user sent led as a query parameter
if (“value” in request.query) {
// if they did, and led=1.. set our variable to 1
if (request.query.value == "1" || request.query.value == "0") {
// convert the led query parameter to an integer
local ledState = request.query.value.tointeger();
// send "led" message to device, and send ledState as the data
device.send("led", ledState);
}
}
// send a response back saying everything was OK.
response.send(200, "OK");
// register the HTTP handler
http.onrequest(requestHandler);`
If you want to use Pitchfork, I have example code on my GitHub page, but you’ll need to have an understanding of how JSON works, and how the agent and device send messages to each other. Little Devil will work very similarly to Pitchfork.
Is there a way to print out that request table in a human-readable form? I'm just getting hex values right now. I've tried decoding it with no luck. Is there anything that better documents all the methods and properties the request object has? I only know there's a query (request.query) from the example.
The HTTP Handler documentation (which is linked to from the http.onrequest page) describes what’s in the request table.
Here’s a function that will recursively traverse tables and arrays and print out all of the values - it can be quite handy:
`function logTable(t, i = 0) {
local indentString = “”;
for(x = 0; x < i; x++) indentString += “.”;
foreach(k, v in t) {
if (typeof(v) == "table" || typeof(v) == "array") {
local par = "[]";
if (typeof(v) == "table") par = "{}";
server.log(indentString + k + ": " + par[0].tochar());
logTable(v, i+4);
server.log(par[1].tochar());
}
else {
server.log(indentString + k + ": " + v);
}
}
@beardedinventor. I might be slightly confused as to what you are asking. The example above is from the Docs page… I just changed “led” to “value” in a few places to make it work with IoT Buddy.