Dynamic Flash

Confessions of a serial code abuser
  • rss
  • Home
  • MTASC
  • Archives
  • About me
  • Goodies
    • Base64 encoder/decoder class
  • My Bookshelf
  • My Talks

Upgrading your app to AIR 1.5

Monday, 17 November 2008

In attempting to upgrade the AirShows application to use the new AIR 1.5 runtime, I tried to simply change the target SDK to Flex 3.2 in the project settings. However, this was giving me all sorts of strange errors, from not launching to displaying the following error dialog when trying to launch the application in debug mode:

Flex Builder 3 - Air 1.5 Error Message

On a hunch, I checked the application configuration file AirShows-app.xml file and noticed that it uses the following namespace declaration:

<application xmlns="http://ns.adobe.com/air/application/1.0">

Since the Flex 3.2 SDK actually expects to be building AIR 1.5 applications, I had a feeling that this was the root cause of my problem. A quick change in the namespace to the following solved my problem:

<application xmlns="http://ns.adobe.com/air/application/1.5">

Thankfully I didn’t waste a whole lot of time trying to figure this out, but I do wish Adobe had provided slightly more useful error messages.

Comments
Comments Off
Categories
Adobe AIR
Tags
Adobe AIR, air, Flex 3
Comments rss Comments rss
Trackback Trackback

The trouble with Flash and REST

Tuesday, 11 November 2008

I find it shocking that the Flash Player still lacks the ability to properly interact with RESTful web services. I keep running into this issue, and every time I scour the Internet hoping that someone, somewhere has found a solution. Every time I return disappointed, hating the Flash Player just a little bit more.

REST stands for REpresentational State Transfer, and describes a technique to organise a web service into resources that can be uniquely identified by a URI. The actions that can be performed on that resource map directly to the standard HTTP methods: GET, POST, PUT, DELETE, HEAD and OPTIONS. With properly organised resources it’s possible to do virtually everything you can do with an RPC-style web service with REST. The benefit of REST is that it goes with the grain of the web, not against it.

The problem with the Flash Player is that the URLRequest class, which is the basis for all HTTP requests, only supports GET and POST operations1. This is due to a limitation in the NPAPI pseudo-standard used by non-IE browsers that only exposes the ability to make GET and POST requests. Unless NPAPI is updated to allow other HTTP methods, and browser vendors and plugin authors update their implementations to match that new API, we need to find other ways of interacting with RESTful web services.

Now, If you’re lucky, the web service you’re trying to interact with will support some way of overriding the request method, such as a X-HTTP-Method-Override header. If this is the case, you can use the requestHeaders property of your URLRequest object to set the appropriate method:

// Create request object with appropriate resource URL
var req:URLRequest = new URLRequest( 'http://rest.example.com/v1/book/1234' );
 
// Use POST because DELETE is non-idempotent
req.method = URLRequestMethod.POST;
 
// Specify DELETE method in X-HTTP-Method-Override header
req.requestHeaders = [ new URLRequestHeader( 'X-HTTP-Method-Override', 'DELETE') ];

If your web service isn’t quite so forgiving, the only option you have is to use the ExternalInferface class and some JavaScript magic to make requests on behalf of your Flash project and send the response back. It’s an ugly hack, but a developer’s gotta do what a developer’s gotta do. If you’re interested in this workaround, leave a comment and I’ll knock up a simple example.

For what it’s worth, Silverlight fares no better in this regard. This is not surprising since Microsoft are stuck with the same browser plug-in APIs as Adobe, but that doesn’t make it any less annoying for developers on both sides of the fence.


  1. If you’re building Adobe AIR applications, you actually have access to the full gamut of HTTP request methods. See the URLRequestMethod documentation for more information. ↩

Comments
8 Comments »
Categories
Flash
Tags
air, Flash, flex, http, JavaScript, representational state transfer, rest, silverlight
Comments rss Comments rss
Trackback Trackback

FileReference.upload doesn’t support custom headers

Monday, 14 January 2008

At work we’re working on an application that makes heavy use of the FileReference.upload() method to upload files to the server. We recently had a change request to add some custom HTTP headers to the upload request, which I didn’t think would be a problem. I was wrong.

When using the FileReference.upload() method to upload a file to a server, you can specify additional variables to be sent along with that upload request by means of a URLRequest object like so:

// Create new request object and point it at the right URL
var request:URLRequest = new URLRequest();
request.url = "/upload.php";
 
// Specify additional variables to be sent with request as POST data
request.method = URLRequestMethod.POST;
request.data = new URLVariables();
request.data.foo = "bar";
 
// Upload file using data from request object
fileRef.upload(request);

Since you can send custom headers using the URLRequest object’s requestHeaders property, I should have been able to specify custom headers for my file upload request fairly easily:

// Create new request object and point it at the right URL
var request:URLRequest = new URLRequest();
request.url = "/upload.php";
 
// Specify additional variables to be sent with request as POST data
request.method = URLRequestMethod.POST;
request.data = new URLVariables();
request.data.foo = "bar";
 
// Specify additional headers to be sent with request
var header:URLRequestHeader = new URLRequestHeader('X-Test-Header', 'Hello');
request.requestHeaders.push(header);
 
// Upload file using data from request object
fileRef.upload(request);

Sadly, the above code doesn’t work. I checked the raw request and response using Charles and the custom X-Test-Header I was trying to send just wasn’t coming through. I tried a number of different variations of the same code, all with the same result.

The problem, I have just discovered, is that there are a number of restrictions placed on the URLRequest object when used in conjunction with the FileReference methods. One of these restrictions is that the requestHeaders property is completely ignored when using the FileReference.upload() method, so it’s impossible to send custom HTTP headers along with a file upload.

To be completely fair to Adobe, this and the other restrictions are stated quite clearly in the documentation for the URLRequest object:

The FileReference.upload() and FileReference.download() methods do not support the URLRequest.requestHeaders parameter.

Since I can’t see any security or technical reason why the URLRequest object should be restricted in this way, I’m off to file a bug with Adobe to see if we can get this addressed in a future version of the Flash Player.

Lesson learned: RTFM.

Comments
8 Comments »
Categories
ActionScript, Flash, Flex 2, Flex 3
Tags
ActionScript, air, file upload, filereference, Flash, flex, urlrequest
Comments rss Comments rss
Trackback Trackback

About Dynamic Flash

Steve Webster is a Senior Web Developer for Yahoo! in London, UK.

He is more than a little concerned that he defines himself in terms of his career, and that he talks about himself in the third person.

Find out more

del.icio.us-ed

  • samuel's squawk at master - GitHub
  • Pixelwave - A native 2D iPhone framework, based on the Flash API
  • Pixelwave - A native 2D iPhone framework, based on the Flash API
  • mnot’s Weblog: Are Resource Packages a Good Idea?
  • Download details: IE App Compat VHD
  • ZSync
  • jQuery source viewer
  • Penetration testing tools - Stack Overflow
  • Logrep
  • DOM Window (jquery.DOMWindow.js)

Recent Posts

  • Moving on
  • iPhone / iPod Touch Development Resources
  • Upgrading your app to AIR 1.5
  • Motivate yourself by doing it in public
  • The trouble with Flash and REST
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox