HTTPService & Long Parameter Strings

I ran into a problem recently with passing very long strings in the url property of an HTTPService component in Flex.   What I would normally do is create a JSON string from nested objects and then pass that string to the server in the url properties.  The component setup would look something like this:


var dataObject:Object = new Object();
dataObject.username = "username";
dataObject.password = "password";
dataObject.type = "LoginObject";

var user:Object = new Object();
user.user = dataObject;
var dataString:String = JSON.encode(user);
JSONService = "http://yourserviceurl?data=' + dataString;
JSONService.send();

This would normally work great, but not if the dataString variable is longer than 12000 characters (12KB) in length. This is because sending variables along the url property of the HTTPService command is the same as sending data on a query string. There is a limit to how much you can actually send. The Flex error is an ambiguous “2038 File I/O Error” that throws back the long string you tried to send. After some correspondence with Tracy Spratt (Adobe engineer?) on FlexCoders group, I set up my service to explicitly pass the dataString using HTTPService tags. This is outlined in the Flex 2 Developers guide, but its not exactly descriptive in when you would apply this method. Here is the new service call with some suggestions from Tracy:


var dataObject:Object = new Object();
dataObject.username = "username";
dataObject.password = "password";
dataObject.type = "LoginObject";

var user:Object = new Object();

user.user = dataObject;
var dataString:String = JSON.encode(user);

var oRequest:Object = new Object();
oRequest.data = dataSring;

JSONService = "http://yourserviceurl";
JSONService.send(oRquest);

This works great with JSON because explicit parameters sent with HTTPService tags use name value pairs, but must be a simple base type. This means you can’t send a complex nested objects, but you can send the encoded JSON string in a simple name value pair within the send()command itself.

This resolved the error and got me back on the correct path. I guess this is one of those mistakes you would not normally find or think about because sending 12000 characters on a string is out of the norm, but I wish I could find some mention of this limitation in the Flex documentation.

One Comment

  1. I have similar code:
    [as]
    var xml:XML=xml; // XML data
    var data:String=”binary JPEG data from a ByteArray”;

    var h:HTTPService = new HTTPService();
    h.method = “post”;
    h.url = “process_xml.php”;
    h.send({xml:xml, data:data});
    [/as]

    so if you didn’t understand, I’m sending XML and JPEG data to a PHP script via POST. However, when the size of [as]data[/as] is around 50kB, then PHP doesn’t receive the data. It simply isn’t in the POST data. I read your conversation on the Yahoo blog, but it didn’t answer my question. Just wasn’t sure if you (or any other reader) had any suggestions.

    Thanks, mister.

Comments are closed.