REST-style web service requests

Flash Player 9 and later, Adobe AIR 1.0 and later

REST-style web services use HTTP method verbs to designate the basic action and URL variables to specify the action details. For example, a request to get data for an item could use the GET verb and URL variables to specify a method name and item ID. The resulting URL string might look like:

To access a REST-style web service with ActionScript, you can use the URLRequest, URLVariables, and URLLoader classes. In JavaScript code within an AIR application, you can also use an XMLHttpRequest.

Programming a REST-style web service call in ActionScript, typically involves the following steps:

  1. Create a URLRequest object.

  2. Set the service URL and HTTP method verb on the request object.

  3. Create a URLVariables object.

  4. Set the service call parameters as dynamic properties of the variables object.

  5. Assign the variables object to the data property of the request object.

  6. Send the call to the service with a URLLoader object.

  7. Handle the complete event dispatched by the URLLoader that indicates that the service call is complete. It is also wise to listen for the various error events that can be dispatched by a URLLoader object.

For example, consider a web service that exposes a test method that echoes the call parameters back to the requestor. The following ActionScript code could be used to call the service:

function restServiceCall() 
    //Create the HTTP request object 
    var request = new air.URLRequest( "" ); 
    request.method = air.URLRequestMethod.GET; 
    //Add the URL variables 
    var variables = new air.URLVariables(); 
    variables.method = "test.echo"; 
    variables.api_key = "123456ABC"; 
    variables.message = "Able was I, ere I saw Elba.";         = variables; 
    //Initiate the transaction 
    window.requestor = new air.URLLoader(); 
    requestor.addEventListener( air.Event.COMPLETE, httpRequestComplete ); 
    requestor.addEventListener( air.IOErrorEvent.IOERROR, httpRequestError ); 
    requestor.addEventListener( air.SecurityErrorEvent.SECURITY_ERROR, httpRequestError ); 
    requestor.load( request ); 
function httpRequestComplete( event ) 
    air.trace( );     
function httpRequestError( error ){ 
    air.trace( "An error occured: " + error.message );     

In JavaScript within an AIR application, you can make the same request using the XMLHttpRequest object:

<head><title>RESTful web service request</title> 
<script type="text/javascript"> 
function makeRequest() 
    var requestDisplay = document.getElementById( "request" ); 
    var resultDisplay  = document.getElementById( "result" ); 
    //Create a conveninece object to hold the call properties 
    var request = {}; 
    request.URL = ""; 
    request.method = "test.echo"; 
    request.HTTPmethod = "GET"; 
    request.parameters = {}; 
    request.parameters.api_key = "ABCDEF123"; 
    request.parameters.message = "Able was I ere I saw Elba."; 
    var requestURL = makeURL( request ); 
    xmlhttp = new XMLHttpRequest(); request.HTTPmethod, requestURL, true); 
    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) { 
            resultDisplay.innerHTML = xmlhttp.responseText; 
    requestDisplay.innerHTML = requestURL; 
//Convert the request object into a properly formatted URL 
function makeURL( request ) 
    var url = request.URL + "?method=" + escape( request.method ); 
    for( var property in request.parameters ) 
        url += "&" + property + "=" + escape( request.parameters[property] ); 
    return url; 
<body onload="makeRequest()"> 
<div id="request"></div> 
<div id="result"></div> 

// Ethnio survey code removed