Flex / AS3 Library : RESTService

Lab Project

One of the most lamented issues in the Adobe community has been Flash Player’s lack of true HTTP support. In case that issue is unfamiliar to you, here’s the rundown: Because the Flash Player VM can run in a variety of browsers and operating systems, it was deemed too complex to try and accommodate all the various permutations of how the VM should react to the potential outcome of making an HTTP request. This situation creates something called a crippled-client.

In the Flash Player’s case, it can only handle responses of type 200, not 200 series, (e.g. 201 Created, 202 Accepted, etc.), just 200. Anything other than a 200 is treated as a 500. No message is passed to the Flash Player, instead the VM throws a completely worthless IO or Stream error. Also, regardless of the success or failure of a request, there are no response headers. Yet another issue is that request headers cannot be passed on anything other than a POST. This requires that all services must support some type of crippled-client protocol, in which every request from said client is a POST, with request headers that tell the service what the client is actually attempting to do.

The solution to this problem is to communicate directly over sockets. This is fine for requests made over HTTP, but since, until recently, Flash had no support for SSL, there was no way to make requests over HTTPS. Enter as3crypto, a cryptography library that supports SSL. Using as3crypto, we’ve developed a small library that can make fully aware HTTP requests using sockets.

There is, unfortunately, a caveat. Adobe has recently decided to make it near impossible to use sockets from web delivered applications. If you’re interested in jumping through the hoops necessary to get sockets working, be sure to let us know about your solution. AIR, however, has no such restrictions on sockets and you should be able to use the RESTService component to make fully aware HTTP requests. So have it!


What is this?

The RESTService class makes fully aware HTTP service calls. Fault and Result events contain all response headers and status messages, as well as the response body.

How Do I Use It?

To use RESTService in your Flex project, just follow these simple steps:

Download The Code

Click on the icon below to download the source code:

Download

The file, arc90restlib.zip, contains a compiled library, the source, and ASDoc generated documentation.

Add the code to your Flex or AIR project

You can either add the compiled library, arc90restlib.swc, to your project’s library path, or include the source directly in your project.

Create a RESTService using either MXML or ActionScript.

MXML:


ActionScript:

import com.arc90.rpc.rest.RESTService;
var service:RESTService = new RESTService("http://example.url/service/");
service.contentType = RESTService.CONTENT_TYPE_XML;
service.method = RESTServiceMethod.POST;
service.resultFormat = RESTService.RESULT_FORMAT_XML;
service.request = ;
service.url = service.rootURL + "resource";
service.headers = {"x-some-header": "somevalue"};
RESTService functions just like mx.rpc.http.HTTPService, the one major difference being that it returns void when calling the send method.

In order to handle events, add event listeners to the RESTService instance.

service.addEventListener(FaultEvent.FAULT, faultHandler);
service.addEventListener(ResultEvent.RESULT, resultHandler);
service.send();
Handling Authentication

There are two ways to handle Basic HTTP Authentication.

  1. Pass an Authorization header:
    service.headers = {Authorization: "Basic "};
  2. Use the setCredentials method:
    service.setCredentials(, );

Documentation

Review the complete documentation for a full list of all properties, methods, and events exposed by RESTService.

Licensing

This arc90 tool is licensed under the Creative Commons Attribution 2.5 license.

Discuss RESTService

You can offer feedback on RESTService at the arc90 blog.