HTTP Functions
ESP.httpGET
Connect to web and get contents. It’s equal to use Ajax on JavaScript.
Syntax:
ESP.httpGET(url, options)
- url is the full address of the page or API address on your device.
- options is an object containing options for http connection.
ESP.httpGET returns a session_id that can be used to cancel the connection if necessary.
Supported options are:
Option | Description |
---|---|
username | If you need to authenticate to the url use this for username. |
password | Authentication password |
timeout | Timeout for the operation in seconds, default is 60. |
user_agent | Sets the User-Agent http header. |
header-* | Sets value of any header that you need, i.e. header-Content-Type. |
success | A function that will be called when operation completed successfully. Syntax of the function is function(result){} |
error | A function that will be called if operation failed. Syntax of the function is function(errorMessage){} |
Example:
ESP.httpGET('https://somedomain.com/',{
'username': 'admin',
'password': '1234',
'header-Content-Type': 'text/plain',
'success': function(result){
// Do something with result
},
'error': function(errorMessage){
// show error message
}
});
ESP.httpPOST
Connect to web and get contents using a POST request.
Syntax:
ESP.httpPOST(url, options)
- url is the full address of the page or API address on your device.
- options is an object containing options for http connection. Options are same as
ESP.httpGET
.
ESP.httpPOST returns a session_id that can be used to cancel the connection if necessary.
Supported options are:
Option | Description |
---|---|
username | If you need to authenticate to the url use this for username. |
password | Authentication password |
timeout | Timeout for the operation in seconds, default is 60. |
user_agent | Sets the User-Agent http header. |
header-* | Sets value of any header that you need, i.e. header-Content-Type. |
post | An object containing key-values that you would like to post. |
success | A function that will be called when operation completed successfully. Syntax of the function is function(result){} |
error | A function that will be called if operation failed. Syntax of the function is function(errorMessage){} |
Example:
var connection_id = ESP.httpPOST('https://mydomain.com/device/api',{
'username': 'admin',
'password': '1234',
'header-Content-Type': 'text/plain',
'post': {
'my_first_param': 'test1',
'my_second_param': 'test2'
},
'success': function(result){
// Do something with result
},
'error': function(errorMessage){
// show error message
}
});
ESP.httpCancel
Cancels a connection that is not yet complete.
Syntax:
ESP.httpCancel(session_id)
- session_id is the return value of the ESP.httpGET or ESP.httpPOST functions.