IoT Buddy, Pitchfork, Little Devil Failing to Toggle LED

I’m following the example:
https://electricimp.com/docs/gettingstarted/3-agents/

But only seem to be able to toggle a pin by sending a request via a browser (testing with mobile version Chrome right now).

https://agent.electricimp.com/{MY_AGENT}?led=1

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");

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// 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.

Thanks for clarifying what IoT buddy sends @jwehr! Starting to get a feel for squirrel and the imp APIs.

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);
    }
}

}

http.onerequest(function(req, resp) {
try {
logTable(req);
resp.send(200, “OK”);
} catch (ex) {
resp.send(500, "Error: " + ex);
}
}`

If you pass your request objects into it, you should get a good idea of everything available in that particular request.

@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.

https://electricimp.com/docs/gettingstarted/3-agents/

All IoT Buddy does is ask you for you agent URL, and a value for each control and then sends it like this:

https://agent.electricimp.com/{MY_AGENT}?value={MY_CONTROLS_VALUE}

So, no table or array data… just the query. Built for the HTTP-IN node.

Am I making sense? My HTTP knowledge has a lot of gaps.

That was in response to some of @stellios’ questions…

What is in the request object, and how to print out all the values in a table object.

Doh! My bad.