Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-19661

Android: 5.0.0 GA SDK HTTPClient Fails to POST Form Data with File Correctly

    Details

    • Story Points:
      1
    • Sprint:
      2015 Sprint 20 SDK

      Description

      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:

       
          var data = {
              "entry.id": entryBean.id
              , "_eventName": "uploadPhoto"
              , "imageData": selectedImage
              , "caption": $.caption.value
              , "fileName": getFileNameFrom(selectedImage)
          };
      
      

      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:

      --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--
      
      

      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:

       
         var data = {
              "entry.id": entryBean.id
              , "_eventName": "uploadPhoto"
              // , "imageData": selectedImage
              , encodedImage: Ti.Utils.base64encode(selectedImage).toString()
              , "caption": $.caption.value
              , "fileName": getFileNameFrom(selectedImage)
          };
      
      

      Of course, this only works if you also control the server-side and can handle the base64 encoded file appropriately.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                msamah Ashraf Abu (Inactive)
                Reporter:
                ryan@asleson.net Ryan Asleson
                Reviewer:
                Chee Kiat Ng (Inactive)
              • Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-20455
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Source Code