1 Reply Latest reply on Oct 10, 2012 4:18 AM by Timothy Brand-Spencer

    Multipart/form-data using HTTPService, sending a binary file and some text in the same request.

    arifali_007

      Hi There,

               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, that's published here.

      https://build.phonegap.com/docs/write_api

       

       

      here is the description of webservice

      ===========

      1) I have to do a post on POST https://build.phonegap.com/api/v1/apps

      2) content type has to be "multipart/form-data"

      3) JSON bodies of requests are expected to have the name 'data'

       

       

        data will be someting like this

      'data={"title":"API V1 App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}'

       

       

      4) include a zip file in the multipart body of your post, with the parameter name 'file'.

      ===========

       

       

      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 read on W3.org( http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 )

      How is text + binary data can be sent through "multipart/form-data" post requst.

      there has to be some boundries.

       

      After this I read and example in flex and  tried following it.

      http://codeio.wordpress.com/2010/04/03/5-minutes-on-adobe-flex-mimic-file-upload-for-in-me mory-contents/

      but it doesn't seems 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

                                              trace(e.result);

                                              var result:String = e.result.toString();

                                              var data:Object = JSON.parse(result);

                                              cb(data.link);

                                    });

                                    service.addEventListener(FaultEvent.FAULT, defaultFaultHandler); //todo : allow user to add his own as well

                                    authAndUploadNewApp(service,appFile);

                          }

       

       

                          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();

                                    stream.open(appFile, FileMode.READ);

                                    var binaryData:ByteArray = new ByteArray();

                                    var fileData : String = new String();

                                    stream.readBytes(binaryData);

                                    stream.close();

                                    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;

                                    service.send();

                          }