Last active
August 29, 2015 14:08
-
-
Save electricimp/d2a36ad23dbfde167404 to your computer and use it in GitHub Desktop.
Agent-to-Internet Outgoing HTTP Request Recipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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