Dynamic Flash

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

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

<head> starts today – get a FREE ticket

Friday, 24 October 2008

<head> kicks off today, and I’ve got 13 free tickets to give away. Simply leave a comment (not forgetting to fill in your email address) and I’ll send a ticket to the first 13 people to respond.

Comments
21 Comments »
Categories
Flash, General
Tags
conference, Flash
Comments rss Comments rss
Trackback Trackback

Come see me speak at <head>

Wednesday, 22 October 2008

<head> web conference: October 24-26, 2008

<head> starts in just under 48 hours time. I am taking part in a ’state of Flash’ roundtable discussion with Seb Lee-Delisle and Carlos Ulloa live on stage at the London hub (Friday at 17:10 BST). Other speakers at the London hub include: Tim O’Reilly, Richard Moross, Jeremy Keith, Paul Boag, Simon Willison, Chris Heilmann, Stephanie Troeth, Ann McMeekin, Gavin Starks, Gavin Bell, and Simon Wardley.

If you want to attend any of the local hubs (expecially the London hub) you’ll need to reserve a place as there is limited seating. If you have your ticket, head on over to the Local Conference Hubs page, sign-in, select your hub and click the link to reserve your place. Don’t forget to print your ticket and bring it along with you on the day.

I’m also giving a virtual session in Room 3 on Sunday at 19:00 BST entitled ‘Beg, Borrow or Steal: The Art of Flashing Without Flashing’. Here’s the session description:

HTML5 is awesome. Well, it will be awesome when it’s finally ready. Probably. The bad news is that by the time the W3C have finished monkeying with HTML5, I’ll probably have given up on the web as a whole and taken up meat goat farming. The good news (for you, me and the goats) is that there’s no need to wait for a lot of the functionality that HTML5 promises; we can start using them right now. In this session I’m going to show you how you can steal these features from the Flash Player and use them in your standards-based sites or applications, without even a sniff of Flash on the page.

See you at <head>.

Comments
2 Comments »
Categories
Flash, General
Tags
conference, Flash
Comments rss Comments rss
Trackback Trackback

FotB ‘08 Schedule: iCal feeds

Sunday, 28 September 2008

In preparation for the start of Flash on the Beach ‘08 tomorrow, I have taken the schedule information and concerted it into a number of iCal feeds for your consumption. The events include locations, links and session sescriptions.

  • Subscribe to FotB 08 Schedule: Concert hall
  • Subscribe to FotB 08 Schedule: Corn Exchange
  • Subscribe to FotB 08 Schedule: Pavilion Theatre
  • Subscribe to FotB 08 Schedule: All Venues

Note that I’ve made an educated guess at which sessions will be in which venues based on previous year’s schedules. I have now confirmed the session venues with the official schedule. I will try to keep these up to date with any movements and changes as the conference progresses.

Comments
Comments Off
Categories
Flash
Tags
calendar, fotb08, ical, schedule
Comments rss Comments rss
Trackback Trackback

Read and write local files with Flash Player 10

Tuesday, 15 July 2008

This text is slightly out of date for the release version of Flash Player 10. The data for local files isn’t available in the OPEN event, and you’ll need to wait for the subsequent COMPLETE event to fire before you can read the data.

I have updated the FlexPad example application and it’s source code, and I’ll update the article here shortly.

One of the features that I’m most looking forward to in Flash Player 10 (codenamed Astro) is local file access. Once the pipe dream of web application developers the world over, the next version of the Flash Player will allow you to read from and write to local files using a simple ActionScript interface.

This is all implemented with a few simple additions to the FileReference class. Reading the content of a file is accomplished with the FileReference.open() method, which can be called once the user has selected a file using the browse dialog. Like most things in the Flash world file loading happens asynchronously, so you’ll have to listen out for the Event.OPEN event. Once the file has loaded, the file content is available as a ByteArray object through the FileReference.data property to do with as you please.

package com.dynamicflash.examples {

public class LocalFileAccessExample {

public function LocalFileAccessExample():void {
  var fileRef = new FileReference();
  fileRef.addEventListener( Event.SELECT, onFileSelect );
  fileRef.addEventListener( Event.OPEN, onFileOpen );
  fileRef.browse();
}

private function onFileSelect( event:Event ):void {
  var fileRef:FileReference = event.target as FileReference;
  fileRef.open();
}

private function onFileOpen( event:Event ):void {
  var fileRef:FileReference = event.target as FileReference;
  var data:ByteArray = fileRef.data as ByteArray;
}

} }

Saving data to a local file is as simple as calling the new FileReference.save() method and passing the data you want written to the file and the filename as parameters. As data you can pass either a String or a ByteArray object.

fileRef:FileReference = new FileReference();
fileRef.save( 'Here is some text', 'some.txt');

In order to make sure that nefarious Internet denizens can’t mess with a user’s files without a their knowledge, the Flash Player pops up a native operating system save dialog every time you try to write data to a file.

Astro's file save dialog

Consequently the filename you pass to FileReference.save() is little more than a suggestion to the user, and the default directory for the saved file seems to be the last save directory, rather than the directory the file was loaded from. I think this is a decent trade-off between functionality and security, even if it means that saving data to local files requires a little more effort on the user’s part than with traditional desktop applications.

Note: There is currently a bug in the latest beta of Flash Player 10 where any attempt to overwrite an existing file will result in that file being truncated to zero bytes, rather than being filled with the data you specified. This is a known issue, and you can track the bug over at Adobe’s issue tracker.

To illustrate this new feature, I’ve knocked up a simple application (including source code) that I’m somewhat unimaginatively calling FlexPad for you to play around with.

If you want to build your own projects that make use any of the new features in Flash Player 10, you’ll need to grab a recent stable copy of the Flex SDK. See Targeting Flash Player 10 Beta with Flex SDK 3.0.x for more information.

Comments
14 Comments »
Categories
ActionScript, Flash, Flex 3
Tags
actionscript3, astro, Flash, flex, tutorial
Comments rss Comments rss
Trackback Trackback

« Previous Entries

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