16Jan Object to URLVariables
950 views
Looking 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.
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

January 18th, 2009 at 11:36 am
This is great, thanks.
March 18th, 2009 at 3:19 am
Thanks, very usefull.
May 9th, 2009 at 8:54 pm
If I’m not mistaken, this function could be used to transfer values from any type of object to another, as long as the types match, right? Just change the type of the var “parameters” to whatever type is holding the data and change paramsToSend to whatever type you want to hold the data.
I found this great function when searching for a method to change the URLVariables object that I am getting back from the URLLoader’s complete event.result.data to a custom type that I have that uses the data returned from the server.
May 9th, 2009 at 8:56 pm
uh, that should be
“that I am getting back from the URLLoader’s complete event.currentTarget.result to a custom type”
January 21st, 2010 at 6:53 am
So what if i need to pass an object with a nested structure?…The code doesn’t seem to recursively traverse the given value.