INCLUDE_DATA

Flex REST client for Assembla.com

The one of the most important feature of Adobe Flex is ability to quick and seamless integration with server side code. Obviously you can simple integrate your flex application by calling directly exposed web service by SOAP method. SOAP is great but it has one big disadvantage: SOAP only described what the messages looked like and there is a space for client implementation/misinterpretation. Sometimes, you can spend dozen frustrating hours on finding small differences between Microsoft SOAP and Java SOAP. So, this time I will demonstrate how to write a simple REST client. Yet, another approach how to integrate client and server.

REST stands for “Representational State Transfer? and in fact, it allows your client application to communicate with server via XML. REST enabled servers are exposing their resources to outside by two parameters URI and HTTP method (POST, GET, PUT and DELETE). Those standard HTTP methods can be mapped as classic CRUD (Create, Read, Upate and Delete) operation in order to modify your data on server side.

My example demonstrates how to read data from REST server (i.e. Assembla.com) and display XML response in Text Area component. Assembla.com exposes a few REST services described in their wikis.

In example below I call two methods in order to read data from server: Get User Profile (returns: user information) and Get Spaces (returns: user?s all projects names and related information). Because Assembla.com services requires basic authentication before call any exposed methods, first I need to prepare authentication headers in my request.

var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest(thisURLTORESTService);
req.method = URLRequestMethod.POST;
req.data = new URLVariables("login=myloginname"); 

var encoder:Base64Encoder = new Base64Encoder();
encoder.encode( "myloginname:mypassword" ); 

var credsHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());
var acceptXMLHeader: URLRequestHeader = new URLRequestHeader("Accept", "application/xml");

req.requestHeaders.push(credsHeader);
req.requestHeaders.push(acceptXMLHeader);			    

loader.load(req);

To run this example client you need to enter your login and password for assembla.com site. Registration on assembla.com is free of charge and more over you can use assembla.com services as a free SVN code repository server (and much more!) for your open source applications.

Click on right click to see whole source code (View Source).

If you do not have your assembla.com account can see how the response from the REST server may look like below.

<?xml version="1.0" encoding="utf-8"?>
<user>
  <id>bC-_kYJwisasas</id>
  <login_name>foobar</login_name>
  <email>foo@foobar.com</email>
  <organization>Foo Software</organization>
  <website>www.foo.com</website>
  <phone>+48 601 111 222</phone>
  <first_im>
    <type>Yahoo</type>
    <id>foo.bar</id>
  </first_im>
  <second_im>
    <type>none</type>
    <id></id>
  </second_im>
</user>

Once you receive xml response object from server you can whatever you want like displaying user information on flex form.