Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-15514] HTTPClient: Disparity between All Platforms for Handling Content-Type Header

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionWon't Fix
Resolution Date2017-06-26T22:13:12.000+0000
Affected Version/sRelease 3.2.0
Fix Version/sn/a
ComponentsAndroid, iOS, MobileWeb
Labelsparity
ReporterBenjamin Hatfield
AssigneeEric Merriman
Created2013-10-17T20:15:03.000+0000
Updated2017-08-24T20:54:39.000+0000

Description

ENVIRONMENT: Mac OS X 10.8.4 Appcelerator Studio, build: 3.2.0.201310141852 (all platforms except MW) Titanium Studio, build: 3.1.3.GA (for MW only) TiSDK 3.2.0.v20131015134843 Android 4.2.2 / API Level 17 iOS 7.0 / Xcode 5 Google Chrome Version 30.0.1599.101 BlackBerry SDK 10.2.0.15 REPRODUCTION: 1. Add/run the server code on your machine with Apache and PHP enabled. I placed mine in my ~/Sites/ directory. 2. Replace with your local IP (and "/~username" if needed). 3. Launch the client code below on the different emulator/simulators/browser and press the Submit button. See the console log output. RESULTS: Content-Type header varies based on the platform and data type sent. On the Android and iOS platforms: * If you are sending a JavaScript object, the content type is set to multipart/form-data. * For all other data types on Android, the content type is set to application/x-www-form-urlencoded. * For all other data types on iOS, the content type is **NOT** set. On the BlackBerry, Mobile Web and Tizen platforms, the content type is always set to application/x-www-form-urlencoded as the default. EXPECTED RESULTS: Platforms should be somewhat more cohesive. SERVER CODE (server.php):
<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: X-Requested-With, Origin, X-Titanium-Id, Content-Type, Accept');
 
if(strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {
    $image = $_FILES[image];
    echo json_encode((object) array('header' => apache_request_headers()));
}
?>


CLIENT CODE (app.js):

    var form_url = 'http://<IP_ADDRESS>/server.php';
 
    var win = Ti.UI.createWindow({
        layout : 'vertical',
        backgroundColor : '#fff'
    });
    var txtTitle = Ti.UI.createTextField({
        top : '80dp',
        value : 'My Photo Title'
    });
    win.add(txtTitle);
      
    var anImageView = Ti.UI.createImageView({
        image : 'images/flower.jpg',
        width : 50,
        height : 50,
 
    });
    anImageView.addEventListener('load', function() {
        Ti.API.info('Image loaded!');
    });
 
    win.add(anImageView);
 
    var btnSubmit = Ti.UI.createButton({
        title : 'Submit'
    });
    btnSubmit.addEventListener('click', function(e) {
        // Send with HTTPClient
 
        var c = Ti.Network.createHTTPClient({
            onload : function() {
                Ti.API.info(this.responseText);
                Ti.API.info("TEXT:   " + this.responseText);
                json = JSON.parse(this.responseText);
                Ti.API.info('json' + json);
            },
            onerror : function(e) {
                Ti.API.info("STATUS: " + this.status);
                Ti.API.info("TEXT:   " + this.responseText);
                Ti.API.info("ERROR:  " + e.error);
            },
            timeout : 50000
        });
        c.enableKeepAlive = false;
        c.open('POST', form_url); 

        //c.setRequestHeader('Content-Type', 'multipart/form-data');
        //c.send(Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "/images/flower.jpg"));
        //c.send('String!');
        //c.send(anImageView.toBlob());
        
        c.send({
            title : txtTitle.value,
            image : Ti.Utils.base64encode(anImageView.image)
        });
        
        
    });
    win.add(btnSubmit);
 
    win.open();

Comments

  1. Lee Morris 2017-08-24

    Mobile Web has been deprecated and will be removed in fixVersion 7.0.0.

JSON Source