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="https://build.phonegap.com/docs/write_api" rel="nofollow">Here is the spec of webservice</a>.
<ol>
<li>I have to do a post on POST <a href="https://build.phonegap.com/api/v1/apps" rel="nofollow">https://build.phonegap.com/api/v1/apps</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:
</li>
<li>include a zip file in the multipart body of your post, with the parameter name 'file'.</li>
</ol>
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="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2" 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="http://codeio.wordpress.com/2010/04...lex-mimic-file-upload-for-in-memory-contents/" rel="nofollow">http://codeio.wordpress.com/2010/04...lex-mimic-file-upload-for-in-memory-contents/</a>
but it doesn't seem to be working for me.
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="https://build.phonegap.com/docs/write_api" rel="nofollow">Here is the spec of webservice</a>.
<ol>
<li>I have to do a post on POST <a href="https://build.phonegap.com/api/v1/apps" rel="nofollow">https://build.phonegap.com/api/v1/apps</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:
Code:
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>
</ol>
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="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2" 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="http://codeio.wordpress.com/2010/04...lex-mimic-file-upload-for-in-memory-contents/" rel="nofollow">http://codeio.wordpress.com/2010/04...lex-mimic-file-upload-for-in-memory-contents/</a>
but it doesn't seem to be working for me.
Code:
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();
}