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

8 actionscript,as3,as3 tutorials,webservices,work  |   March 4, 2008  |     7448

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

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.

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

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.

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.

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

Next, lets define this function and trace the received data

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

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

Cheers,

Martin

Comments
  • March 4, 2008 at 12:51 pm

    Restamon

    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!

  • March 5, 2008 at 12:49 pm

    garrett

    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

  • March 5, 2008 at 1:22 pm

    mlegris

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

  • March 11, 2008 at 6:40 pm

    Tony

    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!

  • March 16, 2008 at 3:17 pm

    izms » Blog Archive » links for 2008-03-16

    […] 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) […]

  • May 31, 2008 at 12:35 pm

    intense creativity » Tutorial - Consuming REST web services in ActionScript 3 - Part 3

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

  • December 9, 2008 at 5:27 pm

    alex

    save to my Bookmarks 🙂

  • April 1, 2010 at 1:22 am

    Royston

    Is there a way to do this is AS2?

Leave a Reply