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.

Sending data
In AS3, there are seven key classes furnished by adobe which we will look at:
flash.net.URLLoaderflash.net.URLRequestflash.net.URLVariableflash.net.URLLoaderDataFormatflash.net.URLRequestMethodflash.events.EventXML
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
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!
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
oops, you’re right. My bad. It’s fixed. Thanks Garrett!
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!
Pingback: izms » Blog Archive » links for 2008-03-16
Pingback: intense creativity » Tutorial - Consuming REST web services in ActionScript 3 - Part 3
save to my Bookmarks
Is there a way to do this is AS2?