ninja_vs_pirate_by_cookiemagikLooking back at my last few blogs, I seem to have been neglecting to post ActionScript related stuff even though I have been more development than ever. It’s probably due to the fact that since I work with it all day that I lack the ambition to blog about about it.
One of the many great things working with Flex is being able to use HTTPService which is a very neat utility. So one day I was working with pure Flash and discovered that URLRequest data property only really allows URLVariables object (and ByteArray for those who do their homework). HTTPService on the other hand can take in raw Object and parse all its properties (including dynamic ones) into a POST payload. With a little digging into HTTPService, I extracted the code it uses to parse and object that I used to build the below function that will convert an Object into a URLVariables object so that you can send it with URLRequest.

Updated: This will work in any version of Flash AS3 now.

public static function objectToURLVariables(parameters:Object):URLVariables {
            var paramsToSend:URLVariables = new URLVariables();
            for(var i:String in parameters) {
                if(i!=null) {
                    if(parameters[i] is Array) paramsToSend[i] = parameters[i];
                    else paramsToSend[i] = parameters[i].toString();
                }
            }
        return paramsToSend;
}


Emoicon (c) to CookiemagiK


Author: Jonathan Dunlap
Jonathan is an experienced software engineer, sole blogger of JADBOX, author of FlashMVC, humanitarian, and has contracted work for Microsoft, Coke, and Disney.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • Furl
  • LinkedIn
  • Pownce
  • Reddit
  • StumbleUpon
  • TwitThis

Tags: