Last active
September 10, 2018 10:33
-
-
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.
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