Making HTTP calls in ActionScript 3

The lingua franca of the web is HTTP, and no programming language can claim to be a web-oriented language if it doesn’t fully support it. Custom APIs and modern protocols are increasingly being built on top of HTTP. Just as it’s rarely a good idea to invent a new XML language, it’s increasingly apparent that HTTP, and more broadly REST, is almost always the most appropriate foundation upon which to build new web APIs.

So how does ActionScript 3 stack up in the area of HTTP support? I’ll (arbitrarily) give a score to AS3′s support for the core elements of a minimal HTTP implementation.

Methods

AS3′s HTTPService class supports the basic HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE), but doesn’t allow custom methods. The WebDAV protocol uses a handful of custom HTTP methods (PROPFIND, PROPPATCH, MKCOL, etc.) which HTTPService would be unable to invoke, so it may be a while before we see an AS3-based WebDAV implementation.

Score: 4/5 (Support for custom methods is missing, but few APIs use them anyhow)

Request headers

HTTPService has a headers property which can hold an arbitrary Array of name-value pairs.

Score: 5/5

Response headers

Unfortunately, there’s no way to extract the headers from an HTTP response. (If you’re interested in verifying this, just make an HTTPService call to a bogus page and set a breakpoint in the fault handler. When execution stops there, examine the FaultEvent in Flex Builder’s Variables window — there’s no evidence anywhere that the error is indeed a 404.) I had a conversation with Matt Chotin of the Flex team about this some months ago, and he indicated that not all browsers expose the headers.

This is a pretty significant limitation, especially since REST principles are motivating the designers of more recent protocols and APIs to leverage the HTTP status code vocabulary to define the semantics of method results, rather than invent new vocabularies in the form of custom responses.

Score: 1/5 (I’ll give it one point, since at least it’s possible to distinguish between successful calls and unsuccessful calls.)

Authentication

HTTPService supports authentication against a remote endpoint via the setRemoteCredentials method. Alternatively, the user’s credentials can be specified in Flex Data Server’s proxy-service.xml file.

Score: 5/5

Overall: 15/20

With the glaring exception of support for response headers, AS3′s support for HTTP is full-featured enough to implement just about any REST-style protocol or API. Let’s hope the Flex and Apollo teams can fill this one remaining hole in a future release.