HTTPIN with JSON data

I have a PHP script posting JSON data to my Electric Imp the following way:

My PHP is:
`

<?php $url = 'http://myImpURL.com'; $myjson = '{"column1":"1","colum2":"2","column3":"3","column4":"4","column5":"5","column6":"6"}'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson); $result = curl_exec($ch); curl_close($ch); ` My Electric Imp: ` // Connect to server imp.configure("Serial RX", [TableIn], []); //Function that waits to receive data class TableIn extends InputPort { function set(v){ data = v.tostring(); server.log(data); } } ` "server.log(data);" only prints the first value" 1", instead of the full JSON data. How do I access data from Column2-6? I've tried `server.log(v.column2);` as in the tutorial, but since the JSON data isn't a table, it doesn't work.

You should avoid using HTTP In nodes + Input Ports, and take a look at http.onrequest instead

You would change your code to look something like this:

http.onrequest(function(request, response) { try { local data = http.jsondecode(request.body); if ("column1" in data) { server.log(data.column1); } // ... } catch (ex) { resp.send(500, "Internal Server Error: " + ex); } });

When I use my agent URL (https://agent.electricimp.com/fGMi6u__LBdg), the page just loads indefinitely (over several minutes) and thus never calls http.ondemand.

Oh, nevermind, it just called it. The connection was just REALLY slow (I just retimed it and it took 60 sec).

Whoops - actually that’s partially my bad.

If you call the agent URL and don’t have a resp.send() the only time it will return is when it times out.

Here’s what the code should look like:

`http.onrequest(function(request, response) {
try {
local data = http.jsondecode(request.body);
if (“column1” in data) {
server.log(data.column1);
}
// …

// when you're done:
resp.send(200, "OK"); // this is a pretty standard response

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

Okay. That fixed it. Thanks:)

I’m trying to do something similar, send 4 values from an iOS app to the imp. If I use http.onrequest, that code goes in the agent, and you are saying that I wouldn’t attach an HTTP-In on the planner?

http.onrequest goes in the agent. If you need to pass values from the incoming http request to the device (you probably do) - you would do so with a device.send / agent.on:

Agent Code
`http.onrequest(function(request, response) {
try {
local data = http.jsondecode(request.body);
// make sure we got all the values we’re expecting
if (“column1” in data && “column2” in data && “column3” in data && “column4” in data) {
device.send(“HttpData”, data);
resp.send(200, “OK”);
}

resp.send(500, "Missing Data in Body");

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

Device Code
agent.on("HttpData", function(data) { //do something with data.. maybe write out to pins or something pin1.write(data.column1); pin2.write(data.column2); pin5.write(data.column3); pin7.write(data.column4); });

Thanks, I do need to. I pretty much figured out the device-agent communication last night, but I need to program it a few times to get it in my head.