Check out our Angular Book Series.

How can I control the parameter order when doing a HTTP POST call from Flex?

I've been working on a legacy AIR application for a client. It integrates with a backend I have no control over. According to their documentation, we need to pass the form parameters in a specific order. This seems unusual for a form post, which contains key-value pairs and is probably not accessed in parameter order. But due to other problems integrating with this service, I decided to see what I could do to specify the parameter order and remove that as a possible point of failure. I wrote about my attempts in this StackOverflow question.

What Didn't Work

This is what we were doing. First, create an HTTPService:


<mx:HTTPService id="service"
showBusyCursor="false"
requestTimeout="240"
method="post" contentType="application/x-www-form-urlencoded"
url="yourURLHere" resultFormat="e4x"
/>

Then, create the parameter object in code:


var parameters : Object = new Object();
parameters.firstParameter = "firstOne";
parameters.amount = 100;
parameters.otherParameters = "Other Random Misc Data";
parameters.lastParameter = "LastOne";

When the parameter object is created for the service call, I added the parameters to the object in order.

Then make the call:


var call : Object = this.service.send(parameters);
call.addResponder( this.responder );

You may have already guessed why this won't work. Object parameters, or keys in a dictionary, are by definition, not in any specific sort order. When the Flex code looped over all parameters to add them to the outgoing service, it was accessing them in alphabetical order. However, that is not something I would bet my life on happening every single time due to the unordered nature of object properties. I also noticed that the outgoing request in the Flash Builder network monitor had the parameters in yet another order. Something with them changed between the Flex code and the server hit.

So, what am I to do?

Using Lower Level APIs to make this work

The final solution was to use lower level APIs. I created the request using URLRequest and URLLoader classes. Instead of passing an object to the request I also had to manually create the request body which is very similar to a URL parameter string.

First, create the parameter string:


var parameters : String = '';
parameters += "firstParameter=firstOne&";
parameters += "amount=100&";
parameters += "otherParameters=Other Random Misc Data&";
parameters+= "lastParameter=LastOne";

Then, create the URLRequest:


var r:URLRequest = new URLRequest(yourURLHere);
r.data = parameters;
r.method = URLRequestMethod.POST;
r.contentType = "application/x-www-form-urlencoded";

The data is set to the parameters string. The method is set to POST and the contentType is set to 'application/x-ww-form-urlencoded'.

And finally, create the URLLoader:


var l:URLLoader = new URLLoader();
l.addEventListener(Event.COMPLETE, myResultMethod);
l.addEventListener(IOErrorEvent.IO_ERROR, myFailureMethod );
l.addEventListener(SecurityErrorEvent.SECURITY_ERROR, myFailureMethod );
l.load(r);

After doing this I could see in the outgoing requests that the parameter order was retained. So, things were good and my issue was solved! This is a lot more tedious than using the HTTPService, but at least I eliminated it as a possible error.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.