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

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.

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

1
2
3
4
var request:URLRequest = new URLRequest("http://www.mydomain.com/feed/myfeed?query=youlike&start=34");
 
var loader:URLLoader = new URLLoader();
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.

1
2
3
4
5
var request:URLRequest = new URLRequest("http://www.mydomain.com/feed/myfeed?query=youlike&start=34");
 
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
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.

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

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

Next, lets define this function and trace the received data

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

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

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

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

Cheers,

Martin

8 comments on “Tutorial – Consuming REST web services in ActionScript 3 – Part 1

  1. Restamon on said:

    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 on said:

    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 on said:

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

  4. 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. Pingback: izms » Blog Archive » links for 2008-03-16

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

  7. save to my Bookmarks :)

  8. Royston on said:

    Is there a way to do this is AS2?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

36,635 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">