NOOB question/ send an curl -X POST when the button is pushed

All,

I am using the Digital Input example ( the “Button”) and the only action I want to do is when the button is push that I can send an external curl -X POST restful command to a external server. Is there a simple way how to do this?

I have tried to use the agent, but I get lost in a hurry. Is there away to have the agent execute the external curl -X POST restful command?

Can the device do this? ( from what I have read the answer should be no, but just checking.)

Thanks

A BIG NOOB

You don’t need the curl -X part, you just make up a request in the agent code with http.post (see documentation) then submit that request with request.sendsync or request.sendasync

The reason is that you are not asking an underlying UNIX operating system to make the request for you with curl as there is no operating system to talk to, just the imp API. The great strength of coding in the agent is that you can make http requests yourself, from the agent to the web, and handle the response. No need to try to use http in the device code either, it can only talk to its agent.

Hope this helps

Here’s some code to get you started (and links at the end):

Device Code
btn <- hardware.pin1; btn.configure(DIGITAL_IN, function() { // if the button was release if (btn.read() == 0) { agent.send("buttonPressed", null); } });

Agent Code
`device.on(“buttonPressed”, function(nullData) {
// Your URL
local url = “http://yourUrl.com/whatever”;

// Any Headers you many need
local headers = {
    "Content-Type": "application/json"
}

// JSON encode the data you're sending
local data = http.jsonencode({ key = "value", anotherKey = 123 });

// make the request, and assign a callback for when it's done
http.post(url, headers, data).sendasync(function(resp) {
    server.log(resp.statuscode + ": " + resp.body);
});;

});`