Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23339] Android http client sets the Content Type header on all requests verbs, causing some servers to time out when expecting a request body

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2016-05-26T21:09:05.000+0000
Affected Version/sRelease 5.3.0
Fix Version/sRelease 5.4.0
ComponentsAndroid
Labelsandroid, httpclient
ReporterRichard Lustemberg
AssigneeAshraf Abu
Created2016-05-06T22:22:15.000+0000
Updated2016-05-26T21:09:10.000+0000

Description

This is a bug found when attempting to connect to a backend running Hunchentoot (Clisp web server), and the CLisp web framework Lucerne. Most other application servers I have experience with wouldn't mind, but this server expects to parse a request body when the Content Type is set. The Android http client, contrary to the iOS one, sets the content type not only on Post/Put/Patch as per the documentation, but actually on all http verbs. The issue can be easily fixed by changing :
if (parts.size() > 0 && needMultipart) {
						boundary = HttpUrlConnectionUtils.generateBoundary();
						client.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
					} else {
						client.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
					}
to
if (parts.size() > 0 && needMultipart) {
						boundary = HttpUrlConnectionUtils.generateBoundary();
						client.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
					} else if (isPostOrPutOrPatch){
						client.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
					}
on itanium_mobile/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java I could send a pull request if you want. Cheers Richard

Comments

  1. Hans Knöchel 2016-05-07

    Community PR: https://github.com/appcelerator/titanium_mobile/pull/7993 [~msamah] fyi
  2. Ashraf Abu 2016-05-09

    [~rlustemberg] Thanks for the PR! Right now, if you need a workaround, you can use this in your code. Hope that works for your sever.:-
       xhr.open("GET", url); 
       xhr.setRequestHeader("Content-Type", "");
       xhr.send();
       
    I'm reviewing your PR and it looks good.
  3. Richard Lustemberg 2016-05-09

    Hi Ashraf.Thanks for the quick handling of the issue! The workaround you mentioned was the first thing we tried. Nevertheless, the web framework we are using (Lucerne) won't have it. Setting the content type header itself to any value will have it expect a request body. Imho it's also a bug on the framework, but the fact remains that the http client was not abiding by the rfc. So in the end we decided to patch the SDK, as we already were maintaining a few other modifications.
  4. Ashraf Abu 2016-05-09

    [~rlustemberg] Code reviewed and merged. https://github.com/appcelerator/titanium_mobile/pull/7993 Thank you!
  5. Lokesh Choudhary 2016-05-26

    Verified the fix. There are no 403 : forbidden errors with the fix. Closing. Environment: Appc Studio : 4.6.0.201605180604 Ti SDK : 5.4.0.v20160526011354 Ti CLI : 5.0.8 Alloy : 1.8.7 MAC El Capitan : 10.11.4 Appc NPM : 4.2.5-5 Appc CLI : 5.3.0-47 Node: 4.2.2 Nexus 6 - Android 6.0.0
  6. Lokesh Choudhary 2016-05-26

    Reopening to correct SDK version

JSON Source