Post from Agent to http endpoint - json with headers

Hello,

this is my first ever post for electric imp as i’m a rank n00b - so please be gentle :wink:

I’m posting some environment readings into the agent from my device, taken from a weather station project that I found in the docs somewhere. All is good here.

however, instead of displaying on a webpage from the agent, I’m trying to get a json object to post to an external http endpoint. (using auth headers etc)…

I understand i can create the http object, with my auth as required using http.get()… then, using http.post post the data object to that endpoint.

My questions though, are around how I can manipulate the stream coming from the device (convert object to JSON)?

And, is there anyway that once converted to json, i can output it somewhere (to file etc) so that i can interrogate the format of the resultant output to configure my endpoint for what to expect?

Firstly, you use http.post() not to send your request but to assemble it. So you set your headers etc. here. This call, like http.get(), returns an httprequest object on which you subsequently call sendasync() or sendsync() to send off the request to your external HTTP endpoint. The former deals with the request and response asynchronously (send the request; get on with something else while waiting for the response; deal with the response when it arrives), while the latter deals with it synchronously (send the request; don’t do anything but wait for the response; deal with the response).

Your code will be something like:

`local body = http.jsonencode( { "message": "Sample Code Test" } );
local headers = { "Authorization": "Basic AbCdEfgh01234",
                           "Content-Type": "Application/JSON" };
local request = http.post(ENDPOINT_URL, headers, body);
local response = request.sendsync();`

As you can see from the first line, http.jsonencode() allows you to convert Squirrel data structures to JSON. In the example, I’ve used a very simple table, but you can convert pretty much any data your device sends to its agent.

You can log your JSON, but to be honest, you know the structure of the data sent by the device, so that’s your template for your handler at the endpoint. The best thing to do is bundle all the sensor data into a single table and send that; the table’s keys and values will translate directly to your JSON. You can even supply table literals in JSON format, as I do in the example above.

Hi @smittyone - thanks for this and this was the exact solution that i deployed in the end :slight_smile:

Curiously though, in reference to the format of the JSON at the end… I’m trying to understand where my format is getting mixed up - something is adding a __meta{} object to the packet…

{
“temp”: 19.3453,
“id”: “20000c2a690ae61b”,
“humidity”: 65.0258,
“lux”: 46.396,
“location”: “CBU Solution Engineering”, //this is the end of what i was generating!
"__meta": {
“receivedTs”: 1465556708842,
“originTs”: 1465556708842,
“ts”: 1465556708842,
“pk”: 16312,
“c”: “au_imp_users_ic001”
}
}

everything after the location tag, well that’s unexpected to say the least. I’m getting this output from another platform (an event engine) that i’m feeding into, when I server.log(jsonEncodedBody) in the agent however, that metadata isn’t there… it’s showing…

{ “temp”: 19.3453, “id”: “20000c2a690ae61b”, “humidity”: 64.5223, “lux”: 42.3991, “location”: “CBU Solution Engineering” }

as expected. My question is, do you think that the imp platform is appending this metadata after the send? Or should I look to my other platform for doing this on the receiving end??

Strangeness abounds…

We’re not appending anything, no. The fact that the meta stuff includes timestamps implies that this is being added by the thing which is receiving your post?