Http.post

Can someone please explain how this http.post stuff works and show an example? Also what do I have to do on my web server side (create a perl program, etc)?

Thanks

This blog was posted only recently.

https://community.electricimp.com/blog/how-to-serve-an-html-form-via-an-agent-and-deal-with-the-results/

Explains how to create a single web-page within agent code… which essentially acts as the web-server. If you need to interact with databases this would then be done via API to 3rd party services such as firebase, for example. If you want to create multiple web pages look at the deploy option within firebase. Then in this case you would use API to send instruction to agent code.

Anyhow blog gives good overview. Here are a few snippets:

This is what creates the html page in the agent…

`
const htmlpage <- @"

My Web Page

Device Control

Explanatory text will go here.

" `

This “htmlpage” will then be referenced within a callback function as defined by http.onrequest. E.g.: http.onrequest(httpHandler);

The “httpHandler” function then monitors all POST’S and GET’S etc depending on method you’ve chosen to use via your html page. Here you usually use the “try” and “catch” methods. The function will have two parameters which define the request and handle the response:

function httpHandler(request,response) { try { local method = request.method.toupper(); if (method == "POST") { // note you need to decode the POST to read the data local data = http.urldecode(request.body) ....... ENTER YOUR CODE HERE ...... // don't forget to send a response response.send(200, "OK") } else { response.send(200, htmlpage); // This displays the webpage as default } } catch(error) { response.send(500, "Internal Server Error: " + error) } }

Within this httpHandler function you can then send data to the imp device etc.

Trust that helps.

Humm…

I guess what I’m trying to figure out is… If I use the following code for the agent:

`
device.on(“senddata”, function(data) {
// Set URL to your web service
local url = “http://www.mywebserver.com/test.cgi”;
server.log(url);

// Set Content-Type header to json
local headers = { “Content-Type”: “application/json” };
server.log(headers);

// encode data and log
//local body = http.jsonencode(data);
local body =“Hello World”;
server.log(body);

// send data to your web service
http.post(url, headers, body).sendasync();
});
`

then on my web server, how do I use this code to get the results, do something on my own web server and then send back a response back to the agent? I guess overall I’m looking for some kind of cgi code to put on my sever that will interact with the above code???

Aha now I see where you are coming from.

I cannot comment about php/cgi/perl web server scripting. Will leave that up to you to do the Google searches.

So in your case the Imp agent will be seen as the web client and for Imp agent to communicate with another web server it will use, as you’ve correctly noted, the http.post method (http://electricimp.com/docs/api/http/post/, or alternatively the http.get method (http://electricimp.com/docs/api/http/get/).

As documentation explains, you would need to send “header” and “body” information to the URL provided.

As your questions relates to “body”, you would usually provide this in the format of “key=value”. This allows you to send multiple parameters if required.

So in your case I believe it would be more common to say local body="myKey=Hello World"

You would then send this body using http.post method as you’ve correctly stated.

Then within your cgi script you would merely read this “myKey” parameter to get the payload.