Modifying request headers in ActionScript 3.0


12.14.07 Posted in actionscript 3, web development by

Though Adobe’s documentation would have you think otherwise, setting or modifying the request headers in an AS3, browser-based Flash application is only possible under certain circumstances. I’m testing this with player version 9,0,115,0 in both Safari 3 and Firefox 2 for OS X.

The documented way to set request headers on an URLRequest object is the following:

var header:URLRequestHeader = new URLRequestHeader("myHeader", "headerValue");
var request:URLRequest = new URLRequest("http://www.[yourdomain].com/");
request.data = new URLVariables("name=John+Doe");
request.requestHeaders.push(header);

However, it seems like you can only modify the headers on a POST request. Which would mean we’d have to specify the POST method (assuming the remote site accepted POST requests for the URL):

request.method = URLRequestMethod.POST;

But even this isn’t sufficient, because the Flash Player will silently convert POST requests into GET requests if the request is empty. So now you have to add URLVariables to the mix:

var variables:URLVariables = new URLVariables();
variables.postVariable = "variableValue";
request.data = variables;

So the only way to successfully set or modify request headers on a URLRequest object is to set its method to POST, and make sure you send at least one variable along with the request. Otherwise the headers will silently remain unchanged.

This is unfortunate as it makes it difficult to connect a browser-based Flash application to a truly REST-ful Web service which determines the markup of content based on request headers.



One Response to “Modifying request headers in ActionScript 3.0”

  1. myrddin says:

    A lot of thanks !
    I have searched for a long time, and you just give me the solution with send the request with a variables !
    Thanks thanks thanks !

Leave a Reply