tags:

views:

52

answers:

1

I'm trying to port an existing AJAX app to Flex, and having trouble with the encoding of parameters sent to the backend service.

When trying to perform the action of deleting a contact, the existing app performs a POST, sending the the following: (captured with firebug)

contactRequest.contacts[0].contactId=2c33ddc6012a100096326b40a501ec72

So, I create the following code:

var service:HTTPService;
function initalizeService():void
{
     service = new HTTPService();
     service.url = "http://someservice";
     service.method = 'POST';
 }
public function sendReq():void
{
    var params:Object = new Object();
params['contactRequest.contacts[0].contactId'] = '2c33ddc6012a100097876b40a501ec72';
    service.send(params);
}

In firebug, I see this sent out as follows:

Content-type: application/x-www-form-urlencoded
Content-length: 77

contactRequest%2Econtacts%5B0%5D%2EcontactId=2c33ddc6012a100097876b40a501ec72

Flex is URL encoding the params before sending them, and we're getting an error returned from the server.

How do I disable this encoding, and get the params sent as-is, without the URL encoding?

I feel like the contentType property should be the key - but neither of the defined values work.

Also, I've considered writing a SerializationFilter, but this seems like overkill - is there a simpler way?

+1  A: 

Writing a SerializtionFilter seemed to do the trick:

public class MyFilter extends SerializationFilter
{
    public function MyFilter()
    {
        super();
    }
    override public function serializeBody(operation:AbstractOperation, obj:Object):Object
    {
        var s:String = "";
        var classinfo:Object = ObjectUtil.getClassInfo(obj);

        for each (var p:* in classinfo.properties)
        {
            var val:* = obj[p];
            if (val != null)
            {
                if (s.length > 0)
                    s += "&";
                s += StringUtil.substitute("{0}={1}",p,val);
            }
        }
        return s;
    }
}

I'd love to know any alternative solutions that don't involve doing this though!

Marty Pitt