Network.sendWebRequest()
Overview
Sends HTTP request to a server and calls callback function when a response is received. Sending request does not block the application.
Added in version 2.1.0
Added support for PATCH method in 2.1.34.
Format
var result = Network.sendWebRequest(url, settings);
                                        | Parameter | Description | Type | Required | 
|---|---|---|---|
| url | URL of the service. | String | Required | 
| settings | Describes the details of the request. | Object | Optional | 
Settings object properties
| Value | Description | Type | Required | Notes | 
|---|---|---|---|---|
| accepts | Data to be sent as Accept header with request. | String | Optional | |
| cache | Specifies whether request can be cached. The default value is true. | Boolean | Optional | |
| complete | Callback function to be called when the request finishes. | Function | Optional | 
                                                     statusCode, success and error callbacks are called first. The function receives two arguments: object A response object with headers, content data and status code. null if a response was not received. Described below. string The status of the request (success, parameterserror, error, timeout or abort).  | 
                                            
| contentType | Specifies type of the data. The default value is application/x-www-form-urlencoded; charset=UTF-8 | String | Optional | |
| data | Data to be sent as request content. | String | Optional | 
                                                     Sending data is only supported with POST, PUT , OPTIONS, and PATCH methods. PATCH was added in version 2.1.34.  | 
                                            
| error | Callback function to be called if request fails. | Function | Optional | 
                                                     To be called when passed parameters are invalid, no response is received, or response with error status code is received. The function receives two arguments: object A response object with headers, content data, and status code. null if a response was not received. Described below. string The status of the request (success, parameterserror, error, timeout or abort).  | 
                                            
| headers | Set of key/value pairs to be sent as additional headers with the request. | Object | Optional | |
| method | The HTTP method to be used with the request. | String | Optional | 
                                                     POST, GET, DELETE, HEAD, OPTIONS, PATCH, PUT and TRACE are supported. Default is GET. PATCH was added in version 2.1.34.  | 
                                            
| password | Password to be used for authentication. | String | Optional | |
| statusCode | Array of status code - function pairs specifying callbacks to be called when response with corresponding status code is received. | Object | Optional | 
                                                     The function receives one argument: object A response object with headers, content data and status code. Described below.  | 
                                            
| success | Callback function to be called if request succeeds. | Function | Optional | 
                                                     To be called only when response with success status code is received. The function receives one argument: object A response object with headers, content data and status code. Described below.  | 
                                            
| timeout | Specifies a timeout (in milliseconds) for the request. The default value is 10000. | Integer | Optional | |
| username | Username to be used for authentication. | String | Optional | 
Response object properties
| Value | Description | Type | 
|---|---|---|
| data | Content data received with the response. | String | 
| statusCode | Status code of the response. | Integer | 
| headers | Array containing all headers as key value pairs. Use .key to get header name and .value to get header value. | Array | 
| headersString | All headers combined in one string. | String | 
Example 1
/* Sends POST request to example server and displays toast messages with received data and headers.
*/
 
function completeCallback(response, textStatus)
{
  if (response != null)
  {
    View.toast("Received data: "+ response.data, true);
    View.toast("Received headers: " + response.headersString, true);
  }
  else
  {
    View.toast(textStatus, true);
  }
}
 
function callback404(response)
{
  /*do something here*/
}
 
Network.sendWebRequest("https://example.com/post", {
  method: "POST",
  data: "This will be sent as message body",
  contentType: "application/x-www-form-urlencoded",
  cache: false,
  headers: {header1: "header1_value", header2: "header2_value"},
  timeout: 8000,
  complete: completeCallback,
  statusCode: {404 : callback404}
});
                                            Example 2
/* Sends GET request to example server and shows Date header from response in toast message.
*/
 
function successCallback(response)
{
  for(var header in response.headers)
  {
    if (response.headers[header].key == "Date")
    {
      View.toast( response.headers[header].key + ": " +  response.headers[header].value, true );
    }
  }
}
 
function errorCallback(data, textStatus)
{
  /*do something here*/
}
 
Network.sendWebRequest("https://example.com/get", {
  method: "GET",
  success: successCallback,
  error: errorCallback
});