Skip to content

HttpRequest Object

The HttpRequest object is used in conjunction with functions in the namespace to send requests to any resource on the network and fetch the response. The HttpRequest object provides the following API elements.

Uploading a File

The HttpRequest API enables you to upload any file to a remote server through the POST method by passing raw bytes of the file. The API helps you get the raw bytes of the file you want to upload.

Note: Before uploading a file, you must select the Enable File Upload check box from the Application Properties > Native > Android tab. For more information, refer Native App Properties for Android.

To upload a file, set the Content-Type parameter as "multipart/formdata" header request. For example:

setRequestHeader("Content-type”, "multipart/formdata");

You can upload the file content in two ways:

  1. Uploading a file by passing the voltmx.types.RawBytes to the send method.

send( voltmx.types.RawBytes)

  1. Uploading a file by passing the key-value pair to the append method of the FormData Object. In the key-value pair, set file name as a key and the voltmx.types.RawBytes object as a value.

Note: When you send raw bytes of the file for upload, the platforms set the Content-Type parameter as "multipart/formdata" by default.

Example for uploading a file

var path = + "/SampleImage.png"
var f1 =;  
var httpclient = new;, “” );  
var frmData= new;  
// sending rawbytes along with file name.  
// Directly sending rawbytes.  

Downloading a File

The HttpRequest API enables you to download any file from a remote server through the GET method. Using the open method, you can initiate the HTTP request by specifying the URL from where you want to download the file. The response will be received in the form of raw bytes (voltmx.types.RawBytes). You can write the received raw bytes to a file using the and the APIs.

Example for downloading a file

var httpclient = new;, “”, false);  
var rb = httpclient.response;  
var myfile = new"/SampleImage.jpg");  
myfile.write(rb, true);  

// Use the following code snippet to download a file with an asynchronous network call.
function downloadImageAsynchronously()
 httpclient = new;, "");   
 httpclient.onReadyStateChange = downloadCallback;   
function downloadCallback()  
 if(httpclient.readyState == 4)  
 var responseContent = httpclient.response;
 var myfile = new"/SampleImage.jpg");
 myfile.write(responseContent, true);
 alert("exception is :: " + err.getMessage());   

Sending a JSON Object and String data as a Request

The HttpRequest API enables you to send a JSON string or JSON object using the POST method. Using the open method, you can initiate the HTTP request with the URL. With the send method of the HttpRequest API, you can directly send the JSON string.

Example for sending a JSON data as a request

var httpclient = new;, “” );  
httpclient.setRequestHeader("Content-Type", "application/json");  
var postdata = {  
"userId": "test"  
"password": "test123"  

Use one of the following:

//Directly sending JSON object  
//Converting JSON object to JSON string and sending  
var jsonStr1 = JSON.stringify(postdata);   
//Sending JSON string  
var jsonStr2= "{\\"userId\\":\\"test\\",\\"password\\":\\"test123\\"}";  

Platform Availability

The HttpRequest API is supported in all platforms except Mobile Web.

Error Codes

100 - Invalid parameter type. The error occurs when you pass other types of data.

101 - Invalid number of arguments. The error occurs when you pass fewer arguments than expected.


  • Setting the Content-Type as non-relevant data results in unexpected behavior. For example, you should not set the "application/x-www-form-urlencoded" as Content-Type when sending the file rawbytes.
  • When uploading a file by passing a key-value pair, set the key-value, as <”filename”, “rawbytes”>. String1 should be file name and String2 should be file raw bytes. Only then the raw bytes will be taken into consideration and sent as “multipart/formdata.” Otherwise, the raw bytes will be ignored.

SPA and Desktop Web Limitations

  • FileSystem or File APIs may not work for SPA and Desktop Web platforms. Only and APIs are available for Desktop Web.
  • Raw bytes cannot be read using the File APIs. You can obtain the raw bytes of any file using the openMediaGallery API (The user must select the file from the device's media gallery).
  • The Send method of the HttpRequest API will not encode the data based on the Content-Type header or data type. You need to encode the data that needs to be sent. By default, the data is encoded in text. Following are a few references help you encode the data:

  • multipart/form-data: Reference Link

  • x-www-form-urlencoded: Reference Link 1, Reference Link 2

  • File raw bytes can be read through the response. The raw bytes cannot be saved to a file using the File APIs. The raw bytes can be downloaded using the method in the Reference Link.


The constants used by HttpRequest APIs are: 

  • constants.HTTP_READY_STATE_UNSENT = 0
  • constants.HTTP_READY_STATE_OPENED = 1
  • constants.HTTP_READY_STATE_LOADING = 3
  • constants.HTTP_READY_STATE_DONE = 4
  • constants.HTTP_METHOD_GET
  • constants.HTTP_METHOD_POST

The delete quotes values are mapped to the corresponding http contentType as below:

ResponseTypeHTTP ContentType
constants.HTTP_RESPONSE_TYPE_TEXTapplication/text, text/plain
constants.HTTP_RESPONSE_TYPE_DOCUMENTapplication/xml, text/xml, text.html, mime type that ends with +xml
constants.HTTP_RESPONSE_TYPE_RAWDATAType except the above values