Project: titanium
Issue TIMOB-19661: Android: 5.0.0 GA SDK HTTPClient Fails to POST Form Data with File Correctly
I am unable to upload data to a remote server using the HTTPClient on Titanium SDK 5.0.0 GA. The same code works correctly on SDK 4.1.0 GA. The data object that is sent to HTTPClient looks like this: {code:javascript} var data = { "entry.id": entryBean.id , "_eventName": "uploadPhoto" , "imageData": selectedImage , "caption": $.caption.value , "fileName": getFileNameFrom(selectedImage) }; {code} Where {{imageData}} is a photo selected from the device. On the server, it seemed like the imageData was coming through, but nothing else: {{entry.id}}, {{_eventName}}, {{caption}}, and {{fileName}} were no populated on my server-side form. In an effort to help debug the issue, I dumped the "raw" incoming POST data to the server console. The results looked something like this: {code:java} --4UVkNoCkrozvPT0TuugeoVAr0YbFCJ Content-Disposition: form-data; name="_eventName Content-Type: ; charset=UTF-8 Content-Transfer-Encoding: 8bit uploadPhoto --4UVkNoCkrozvPT0TuugeoVAr0YbFCJ Content-Disposition: form-data; name="fileName Content-Type: ; charset=UTF-8 Content-Transfer-Encoding: 8bit IMAG0527.jpg --4UVkNoCkrozvPT0TuugeoVAr0YbFCJ Content-Disposition: form-data; name="entry.id Content-Type: ; charset=UTF-8 Content-Transfer-Encoding: 8bit O1wJEoXb1lfhdYfwkn38UawovA4pG6hHecZ6DGv2-Wx0Ww-rmjihfM41D4jyCXs4 --4UVkNoCkrozvPT0TuugeoVAr0YbFCJ Content-Disposition: form-data; name="imageData"; filename="tixhr-1575133492.jpeg" Content-Type: image/jpeg Content-Transfer-Encoding: binary (lots of binary data for the image) --4UVkNoCkrozvPT0TuugeoVAr0YbFCJ Content-Disposition: form-data; name="caption Content-Type: ; charset=UTF-8 Content-Transfer-Encoding: 8bit final. --4UVkNoCkrozvPT0TuugeoVAr0YbFCJ-- {code} I immediately saw the problem. Note how for the non-binary fields, such as {{_eventName}} at the top, the right quote is missing at the end of the name value. This explains why my web framework was unable to correctly populate the data model with the form data; it was unable to read it due to the missing double quote. This is a new regression from 4.1.0. It works correctly if I use the 4.1.0 SDK. For those who are seeing this bug, one possible work around is to convert the file to be uploaded to a base64 encoded string. This is how my example from above updated to use a base64 encoded string: {code:javascript} var data = { "entry.id": entryBean.id , "_eventName": "uploadPhoto" // , "imageData": selectedImage , encodedImage: Ti.Utils.base64encode(selectedImage).toString() , "caption": $.caption.value , "fileName": getFileNameFrom(selectedImage) }; {code} Of course, this only works if you also control the server-side and can handle the base64 encoded file appropriately.
Story points: 1