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.
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.
In AS3, there are seven key classes furnished by adobe which we will look at:
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.
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!
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;
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.
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!
[…] 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) […]
[…] is the third installment of the series, you can find the first two here: Part 1 – http://blog.martinlegris.com/?p=87 Part 2 – […]
save to my Bookmarks 🙂
Is there a way to do this is AS2?