Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active September 10, 2018 10:33
Show Gist options
  • Save ElectricImpSampleCode/00e9248299e6c0c9b0b0 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/00e9248299e6c0c9b0b0 to your computer and use it in GitHub Desktop.
This example shows the creation and transmission of a simple HTTP GET request.
// 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