Skip to content

Instantly share code, notes, and snippets.

@electricimp
electricimp / httpget.agent.nut
Created July 1, 2013 18:18
Basic wrapper for an HTTP Get request. Creates the request, sends it, and returns the response.
// Basic wrapper to create an execute an HTTP GET
function HttpGetWrapper (url, headers) {
local request = http.get(url, headers);
local response = request.sendsync();
return response;
}
@electricimp
electricimp / httpdelete.agent.nut
Created July 1, 2013 18:21
Basic wrapper for an HTTP Delete request. Creates the request, sends it, and returns the response.
// Basic wrapper to create an execute an HTTP DELETE
function HttpDeleteWrapper (url, headers) {
local request = http.httpdelete(url, headers);
local response = request.sendsync();
return response;
}
@electricimp
electricimp / httpput.agent.nut
Created July 1, 2013 18:34
Basic wrapper for an HTTP Put request. Creates the request, sends it, and returns the response.
// Basic wrapper to create an execute an HTTP PUT
function HttpPutWrapper (url, headers, body) {
local request = http.put(url, headers, body);
local response = request.sendsync();
return response;
}
@electricimp
electricimp / httppost.agent.nut
Created July 1, 2013 18:35
Basic wrapper for an HTTP Post request. Creates the request, sends it, and returns the response.
// Basic wrapper to create an execute an HTTP POST
function HttpPostWrapper (url, headers, string) {
local request = http.post(url, headers, string);
local response = request.sendsync();
return response;
}
@electricimp
electricimp / httpjsonencode.agent.nut
Last active February 20, 2016 22:16
Basic example to demonstrate how to encode json data and POST it to an arbitrary web service.
device.on("senddata", function(data) {
// Set URL to your web service
local url = "https://www.mywebservice/object";
// Set Content-Type header to json
local headers = { "Content-Type": "application/json" };
// encode data and log
local body = http.jsonencode(data);
server.log(body);
@electricimp
electricimp / jsondecode.agent.nut
Created July 1, 2013 23:15
Basic example to demonstrate processing an incoming HTTP request and parsing the JSON body.
// create a request handler
http.onrequest(function(request, response){
try
{
// decode the json - if it's invalid json an error will be thrown
local data = http.jsondecode(request.body);
// check if particular keys are in the json body
if ("pin1" in data) {
// send message to device
@electricimp
electricimp / urlencode.agent.nut
Created July 2, 2013 22:55
This example outputs the result of various http.urlencode() calls.
server.log(http.urlencode({ foo="bar" }));
server.log(http.urlencode({ foo="wot?" }));
server.log(http.urlencode({ bar="bar", wurdle=[3,4] }));
server.log(http.urlencode(1234)); // this will throw an error
@electricimp
electricimp / urldecode.device.nut
Created July 2, 2013 22:57
Demonstrates a basic http request handler that uses url-form-encoded data.
hardware.pin1.configure(ANALOG_OUT);
hardware.pin2.configure(DIGITAL_OUT);
// When we get a pin1 message
agent.on("pin1", function(data) {
// Write value to pin1
hardware.pin1.write(data);
}
// When we get a pin2 message
@electricimp
electricimp / httponrequest.agent.nut
Created July 9, 2013 20:45
Simple example of an http handler.
// Log the URLs to turn LED on/off when agent starts
server.log("Turn the LED on by browsing to " + http.agenturl() + "?led=1");
server.log("Turn the LED off by browsing to " + http.agenturl() + "?led=0");
// HTTP Request handlers expect two parameters:
// request: the incoming request
// response: the response we send back to whoever made the request
function requestHandler(request, response) {
// Check if the variable led was passed into the query
if ("led" in request.query) {
@electricimp
electricimp / impconfigure.device.nut
Created July 10, 2013 16:56
The most basic Electric Imp program you can make
imp.configure("Hello World", [], []);