Multipart/form-data Flex HTTPService uploading a file


Staff member
I am new to Flex and also new to writing a client for a web service.
My question is more about Flex (Flash Builder 4.5) APIs, what APIs to use.

I want to access a web service, and create a Flex / AIRwrapper for it,
which anyone can use.

<a href="" rel="nofollow">Here is the spec of webservice</a>.

<li>I have to do a post on POST <a href="" rel="nofollow"></a></li>
<li>content type has to be "multipart/form-data"</li>
<li>JSON bodies of requests are expected to have the name 'data' and will be something like this:

data={"title":"API V1 App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}
<li>include a zip file in the multipart body of your post, with the parameter name 'file'.</li>

I want to make a 'multipart/form-data' Post and send one string and one zip file.

My first question to self was if I send both string + binary data in the body,
how will server understand where string end and where zip file starts?

Then I read how <a href="" rel="nofollow">text + binary data can be sent through "multipart/form-data" post request</a>. There has to be some boundaries.

After this I read and example in flex and tried following it.
<a href="" rel="nofollow"></a>
but it doesn't seem to be working for me.

    public function createNewApp(cb:Function , appFile : File):void 
        var service:HTTPService = new HTTPService();
        service.url = ROOT+"apps";
        service.showBusyCursor = true;
        service.addEventListener(ResultEvent.RESULT, function(e:ResultEvent):void {
            //translate JSON
            var result:String = e.result.toString();
            var data:Object = JSON.parse(result);               
        service.addEventListener(FaultEvent.FAULT, defaultFaultHandler); //todo : allow user to add his own as well

    private function authAndUploadNewApp(service:HTTPService,appFile : File):void {

        var encoder:Base64Encoder = new Base64Encoder();
        encoder.encode(username + ":"+password);
        service.headers = {Accept:"application/json", Authorization:"Basic " + encoder.toString()};
        service.method ="POST";
        var boundary:String = UIDUtil.createUID();
        service.contentType = "multipart/form-data; boundary=—————————" + boundary;
        var stream:FileStream = new FileStream();, FileMode.READ);
        var binaryData:ByteArray = new ByteArray();             
        var fileData : String = new String();
        fileData = binaryData.readUTFBytes(binaryData.bytesAvailable); // I think this is where I have problem.... how do 
                   //how do i converrt this bytearray/stream of data to string and send it in my post request's body - i guess if this step work rest should work..   
        var params: String = new String();
        var content:String = "—————————" + boundary + "nr";
        content += 'Content-Disposition: form-data; name="data";' + '{"title":"ELS test app 2","package":"com.elsapp.captivate","version":"12.3.09","create_method":"file"}' + "nr";
        content += "—————————" + boundary + "nr";
        content += 'Content-Disposition: form-data; name="file";' + fileData  + "nr";
        content += "—————————–" + boundary + "–nr";
        service.request = content;