HTTP API
The HTTP API enables apps to communicate with the server using HTTP protocol. Enables your app to receive HTTP requests directed to URLs and send HTTP responses. The HTTP API is supported on Android, iOS, and Web platforms.
Methods
httpRequest
Use this method to initiate an HTTP request to a server. It helps you to request objects that provide a convenient method to build an HTTP request and send it to the server.
Syntax
var clientObject = $m.httpRequest(options);
clientObject.addPart( fieldId, content, mimeType ); clientObject.addHeader( header, value );
clientObject.send( success_callback, error_callback );
Parameters
options
: (object
). The options of the HTTP request may contain the following properties:
- url: (
String
). URL for which the request must be built. - Headers: (
Object
). An object containing request headers. - Method: (
String
). A string specifying the HTTP request method to use, such as "get" or "post". - success_callback: (
function
). A callback function that provides the object if the request is successful. - error_callback: (
function
). A callback function that provides the error object if the HTTP request can’t be initiated. - timeout: (
Number
). A number specifying the timeout of the HTTP request in milliseconds.
Example
var options = {"url":"https://www.google.com/","type":"POST","timeout":3000,"headers":{"Content-Type":"multipart/form-data"},"data":{"kay1":"value1"},"useproxy":true}
var clientObject = $m.httpRequest(options);
var success_callback = function(res){
// do something
}
var error_callback = function(res){
// do something
}
clientObject.send(success_callback, error_callback)
get
Use this method to send a HTTP GET request to the URL.
Syntax
$m.get( url, options, success_callback, error_callback );
Parameters:
url
: (String
). The URL to which the request should be sent.
options
: (object
). Optional parameters object passed to the request. It can contain one or more of the following properties:
- headers: (
Object
). The headers to be set for the request. - Timeout: (
Number
). The timeout of the request in milliseconds.
success_callback
: (function
). A callback function that is called when the request is completed:
- data: (
String
). The response data received from the server.
error_callback
: (function
). A callback function that provides the error object.
- message: (
String
). A short description of the error. - description: (
String
). The error description in detail if any.
Example
var options = {"timeout":3000,"headers":{"Content-Type":"multipart/form-data"},"data":{"kay1":"value1"},"useproxy":true}
var success_callback = function(res){
// do something
}
var error_callback = function(res){
// do something
}
$m.get(success_callback,error_callback ,"http://10.50.200.106:8555/rest/projects", options)
post
Use this method to send a HTTP POST request to the URL.
Syntax
$m.post( url, options, success_callback, error_callback );
Parameters
url
: (String
). The URL to which the request should be sent.
options
: (object
). Optional parameters object passed to the request. It can contain one or more of the following properties:
- headers: (
Object
). The headers to be set for the request. - Timeout: (
Number
). The timeout of the request in milliseconds.
success_callback
: (function
). A callback function that is called when the request is completed:
- data: (
String
). The response data received from the server.
error_callback
: (function
). A callback function that provides the error object.
- message: (
String
). A short description of the error. - description: (
String
). The error description in detail if any.
Example
var options = {"timeout":3000,"headers":{"Content-Type":"multipart/form-data"},"data":{"kay1":"value1"},"useproxy":true}
var success_callback = function(res){
// do something
}
var error_callback = function(res){
// do something
}
var data = {"username":"alice","password":"!@#123ABCabc"}
$m.post(success_callback,error_callback ,"http://10.50.200.106:8555/rest/projects", data,options)
download
Use this method to send a HTTP request to the specified URL and download the file. If the browser recognizes the file format, the file opens and displays the content in the browser itself. If the file format is not recognized by the browser, the file will be downloaded to your local computer. In case of mobile and tablet devices, the file is always downloaded.
Syntax
$m.download( url, options, success_callback, error_callback );
Parameters:
url
: (String
). The URL to which the request should be sent.
options
: (object
). Optional parameters object passed to the request. It can contain one or more of the following properties:
- headers: (
Object
). The headers to be set for the request. - Timeout: (
Number
). The timeout of the request in milliseconds.
success_callback
: (function
). A callback function that is called when the request is completed:
- data: (
String
). The response data received from the server.
error_callback
: (function
). A callback function that provides the error object:
- message: (
String
). A short description of the error. - description: (
String
). The error description in detail if any.
Example
var options = {"timeout":3000,"headers":{"Content-Type":"multipart/form-data"},"data":{"kay1":"value1"},"useproxy":true,"downloadFile":"file://user/emulate/1/downloads"}
var success_callback = function(res){
// do something
}
var error_callback = function(res){
// do something
}
$m.download( url, options, success_callback, error_callback );
Properties
The following table includes the list of optional parameters that you can use with various methods of HTTP API. The datatype and description of each property are also provided.
Property | Datatype | Description |
---|---|---|
|
String |
Part ID of the request. |
|
String or File |
Content of the HTTP request. |
|
Object |
Header field and value. |
|
Function |
A callback function that provides the response object. |
|
Function |
A callback function that provides the error object. |
|
Integer |
Milliseconds before a request time out. |
|
Integer |
Specifies the types of credentials used for authentication:
|
|
String |
Required when authMode is enabled. |
|
String |
Required when authMode is enabled. |
|
String |
Required when NTLM is enabled. |
|
String |
Required when file downloading. |