Skip to content

Instantly share code, notes, and snippets.

@electricimp
Last active August 29, 2015 14:08
Show Gist options
  • Save electricimp/d2a36ad23dbfde167404 to your computer and use it in GitHub Desktop.
Save electricimp/d2a36ad23dbfde167404 to your computer and use it in GitHub Desktop.
Agent-to-Internet Outgoing HTTP Request Recipe
// Set up outgoing request object
local request = http.get(webServiceURL, webServiceHeaders)
// Define the response handler
function handleResponse(responseTable)
{
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200)
{
// Remote service has responded with 'OK' so decode
// the response's body 'responseTable.body' and headers 'responseTable.headers'
// Code omitted for clarity
}
else
{
// Log an error
server.log("Error response: " + responseTable.statuscode)
}
}
// Send the request asynchronously. This will not block the imp CPU
request.sendasync(handleResponse)
// Set up outgoing request object
local request = http.get(webServiceURL, webServiceHeaders)
// Send the request synchronously. This will block the imp CPU
// until a response has been received from the remote service
local responseTable = request.sendsync()
if (responseTable.statuscode == 200)
{
// Remote service has responded with 'OK' so decode
// the response's body 'responseTable.body' and headers 'responseTable.headers'
// Code omitted for clarity
}
else
{
// Log an error
server.log("Error response: " + responseTable.statuscode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment