Http interactions with Agents

Hi folks,

I seem to not be able to find any documentation on how to handler http incoming requests for agents.

Here is what I have:

http.onrequest(function) can be used to have a function called when a http request is made to the agent.
The function has two parameters (HttpRequest, HttpResponse)

The values within each of these tables to not appear documented.

Request has a body, and you call send on the response. Other than that, it appears to be a black box.

** End goal **
I’m trying to grab the GET parameters from a request.

Ah, have you found our forthcoming new documentation site, maybe? That one’s not quite complete yet. Right now the callback function is documented only at the old site: http://devwiki.electricimp.com/doku.php?id=electricimpapi:http:handler

Peter

Thanks Peter, that was what I needed.

Now, back to awesome impness.

Check out @beardedinventor’s GitHub examples as well. https://github.com/beardedinventor/electricimp/tree/master/Examples/BasicAgent
Also mine, if they are any help:
https://github.com/joel-wehr/pitchfork_control_panel_electric_imp

Inline functions are really cool when you learn how to use them.

First of all, thank you guys for such awesome device and API. I love the imp! Processing that for Arduino I had to do in some app on the web I can do now in the agent. Also, when I needed to check some value in Arduino I had to have it connected to the computer, now I just use the server.log magic and it’s there in the IDE. Really, love it!

I do have a question: I can get the query params from a GET request but how do I do it from a POST request? I used postman to send a POST to my agent URL and I know it works because I get the right response back. But when I try to get the request params I don’t get anything. Both these for loops print nothing:
` foreach(idx,val in request.query)
print(“index=”+idx+" value="+val+"
");

foreach(val in request.query)
print(“value=”+val+"
");`

What am I doing wrong?

Thanks for all your help!

@merlin13 if you open up the javascript console your probably seeing CORS errors in red. Function be low a bit crude but POST body comes as string so needs to be split up to access your POST keys

`function requestHandler(request,res){
res.header(“Access-Control-Allow-Origin”, “*”);

if(request.method=="OPTIONS"){
    res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
    res.header("Access-Control-Allow-Headers", "origin, x-csrftoken, content-type, accept"); 
    res.send(200,"OK.OPTIONS");
}

if(request.method=="POST"){
   
    server.log("body:"+request.body);
    local keys = split(request.body,"&");
    local keyA = split(keys[0],"=");
    server.log("keyA:"+keyA[1]);
    res.send(200,"OK.POST");    
}        

}

http.onrequest(requestHandler);`

I written some tutorials using JQmobile might be of help http://industrialinternet.co.uk/eimp/t2_digital_out/index.html

local keys = split(request.body,"&"); local keyA = split(keys[0],"="); server.log("keyA:"+keyA[1]);
That’s what http.urldecode() is for:
local keys = http.urldecode(request.body); foreach(idx,val in keys) server.log("index="+idx+" value="+val+"\ ");

Peter

Thank you so much, @peter and @controlCloud. It works great! You are awesome, thanks again for all the help. And for the tutorial link, @controlCloud.