Trouble posting json to sen.se

I’m trying to post a value to my sen.se account.
I just can’t seem to figure out the correct way to construct the json,
and whether to use PUT or POST (the difference?). Or perhaps the
problem is encoding? There are no errors on the Agent side, but there
is on the sen.se side. I don’t get a -200- response, and nothing is showing
up on sen.se

Here is the code that compiles ok, but sen.se doesn’t see the data.
I just hard-coded my feed_id and a value.

`// Agent POST
server.log(“Sen.se Agent: v1.0”);

device.on(“impMsg”, function(im) {
local jc="{“feed_id”:30380,“value”:“12345”}";
local body = http.jsonencode(jc);
server.log(jc);
local req = http.put(“http://api.sen.se/events/?sense_key=Kdsf3U_U8SDIQ”,
{“Content-Type”: “application/json”},body);
local res = req.sendsync();
server.log("agent res: "+res.statuscode);
});`

You’re JSON encoding a JSON encoded string.

local jc={ feed_id:30380, value:12345 };

…would be more correct, as then http.jsonencode() will put it in string form.

You don’t need to escape this works
local jc={“feed_id”:30380,“value”:“12345”};
Also I use http://requestb.in/ to test HTTP requests
And http://jsoneditoronline.org/ to validate my JSON

Also see example in wiki http://devwiki.electricimp.com/doku.php?id=electricimpapi:http:jsonencode

Yes!

Thanks once again.

For those like me struggling … here is what works.

The Agent:
`// Agent POST
server.log(“Sen.se Agent: v1.0”);
feedID <-30380;
sensKEY <- “FXQTYIp5aJ_U32q8j6AIEQ”;

device.on(“impMsg”, function(im) {
local dat1=im.data1;
local body = http.jsonencode({“feed_id”:feedID,“value”:dat1});
server.log(body);
local req = http.post(“http://api.sen.se/events/?sense_key=”+sensKEY,
{“Content-Type”: “application/json”},body);
local res = req.sendsync();
server.log("agent res: "+res.statuscode);
});`

The Device (imp)
// send data to server. function send_to_sense() { local data1 = "12345"; agent.send("impMsg",{data1=data1}); }

Data 1 is coming across to sen.se just fine now.

And in a wiki example I saw, they were using http.put( )
I found with sen.se I have to use http.post ( ).

controlCloud,
That devwiki example (the link you gave) … that is a terrible example that they show. How can anyone new to this understand how to use it? Why don’t they explain what the variable ‘x’ looks like? That’s important, so people don’t do what I did (encoding an encode). I really hope as the agents get released to public we can post some easy to understand snippets and examples.

Agree the example is poor! I wish I was better writer and had more time to write up what I’ve learnt. I’ve just spent most my free time this week fecking about with electronics wouldn’t want to write about that :slight_smile: most of time I’m fumberling in the dark that’s why I use tools like request.in

Hugo mentioned something about improving the way code is shown on this forum, and I mean Squirrel, PHP, etc. Once this site improved to allow a place to write-up tutorials and code examples, maybe all of us can gather these posts and make something easy to look at.

Happy to help with that