Tutorial - Consuming REST web services in ActionScript 3 - Part 1

Hello Everyone,

today I will discuss the method I have developed to consume RESTfull web services using AS3. This method was developped over time, I have written many different web service clients since last summer, about 20 of them; for different web services. Some of them require no parameters at all, some of them require GET params only, while others require GET & POST parameters.

Our Goal

The goal is depicted in the following diagram. We would like to have a class that takes care of the request and receives the response, this class would ideally be a Singleton so we can use it from anywhere, and would dispatch custom events when the data is ready for us to use.

Goal of our tutorial: the WSClient Singleton

Sending data

In AS3, there are seven key classes furnished by adobe which we will look at:

  • flash.net.URLLoader
  • flash.net.URLRequest
  • flash.net.URLVariable
  • flash.net.URLLoaderDataFormat
  • flash.net.URLRequestMethod
  • flash.events.Event
  • XML

First we build our request URL using URLRequest

Actionscript:
  1. var request:URLRequest = new URLRequest("http://www.mydomain.com/feed/myfeed");

If you were to pass parameters by GET, you would append them in your url string like in the next code snippet. In this example we pass two params: query & start. The value "youlike" is assigned to query and 34 is assigned to start.

Actionscript:
  1. var request:URLRequest = new URLRequest("http://www.mydomain.com/feed/myfeed?query=youlike&start=34");

To send our request, we will use the URLLoader class like so

Actionscript:
  1. var request:URLRequest = new URLRequest("http://www.mydomain.com/feed/myfeed?query=youlike&start=34");
  2.  
  3. var loader:URLLoader = new URLLoader();
  4. loader.load(request);

It is important to specify the dataFormat of the URLLoader so it knows we are expecting, in our case, xml which is treated like text.

Actionscript:
  1. var request:URLRequest = new URLRequest("http://www.mydomain.com/feed/myfeed?query=youlike&start=34");
  2.  
  3. var loader:URLLoader = new URLLoader();
  4. loader.dataFormat = URLLoaderDataFormat.TEXT;
  5. loader.load(request);

To send POST variables, we use the URLVariables class to specify the variables & values and we modify the method attribute of our URLRequest object, setting it to POST.

Actionscript:
  1. // we are still sending GET variables along..
  2. var request:URLRequest = new URLRequest("http://www.mydomain.com/feed/myfeed?query=youlike&start=34");
  3.  
  4. var loader:URLLoader = new URLLoader();
  5. loader.dataFormat = URLLoaderDataFormat.TEXT;
  6. request.method = URLRequestMethod.POST;
  7.  
  8. var variables:URLVariables = new URLVariables();
  9. // these can be anything, you choose!
  10. variables.vote = "yeah!";
  11. variables.id = 2193;
  12.  
  13. // assign the data to be sent by POST
  14. request.data = variables;
  15.  
  16. // send the request
  17. loader.load(request);

Receiving Data

Now that we covered every aspect of sending data, we will handle the reception of data. When the URLLoader object has received the response, it will dispatch a Event.COMPLETE event. Therefore we will add an event listener for this event just before calling load

Actionscript:
  1. // ...
  2.  
  3. loader.addEventListener(Event.COMPLETE, handleResults);
  4.  
  5. loader.load(request);

Next, lets define this function and trace the received data

Actionscript:
  1. function handleResults(evt:Event):void
  2. {
  3.     var response:String = evt.target.data as String;
  4.     trace("response:"+response);
  5. }

Because the data we expect is in XML format, let's convert it to XML.

Actionscript:
  1. function handleResults(evt:Event):void
  2. {
  3.     var response:String = evt.target.data as String;
  4.     var xmlData:XML;
  5.  
  6.     try
  7.     {
  8.         xmlData = new XML(response);
  9.     }
  10.     catch(error:TypeError)
  11.     {
  12.         trace("the response data was not in valid XML format");
  13.     }
  14. }

In the next installment, I demonstrate how I wrap all this functionality in a singleton!

Cheers,

Martin



Related posts (automatically generated):

  1. Tutorial - Consuming REST web services in ActionScript 3 - Part 2
  2. Tutorial - Consuming REST web services in ActionScript 3 - Part 3
  3. Tutorial - AS3 & REST web services with RESTProxy - Part 1
  4. Tutorial - Consuming REST web services in ActionScript 3 - Part 4


7 Responses to “Tutorial - Consuming REST web services in ActionScript 3 - Part 1”

  1. Restamon says:

    Very nice! I assume “request.method = URLRequestMethod.PUT;” would be a valid approach as well if ever needed. I’ve never played around with AS3, but I may have to take a look one day … thanks!

  2. garrett says:

    in your “post example” shouldn’t:
    loader.data = variables;
    be:
    request.data = variables

    loader.data would contain the data that loader receives whereas request.data would contain the post data sent by loader.

    thanks for the tips!!! much appreciated!!! i was upset when i found sendAndLoad gone and had written my own method to simulate it. i have no idea how i looked over the URLRequest property data.

    be well…
    garrett

  3. mlegris says:

    oops, you’re right. My bad. It’s fixed. Thanks Garrett!

  4. Tony says:

    I have been looking all over to find a simple way to do this, but everything I found was WAY more complex that it needed to be. Your tutorial was exactly what I was looking for!

    Thank you!

  5. izms » Blog Archive » links for 2008-03-16 says:

    [...] intense creativity ยป Tutorial - Consuming REST web services in ActionScript 3 - Part 1 We would like to have a class that takes care of the request and receives the response, this class would ideally be a Singleton so we can use it from anywhere, and would dispatch custom events when the data is ready for us to use. (tags: as3 actionscript webservices flash) [...]

  6. intense creativity » Tutorial - Consuming REST web services in ActionScript 3 - Part 3 says:

    [...] is the third installment of the series, you can find the first two here: Part 1 - http://blog.martinlegris.com/?p=87 Part 2 - [...]

  7. alex says:

    save to my Bookmarks :)

Leave a Reply