[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 Issue | n/a |
---|---|
Type | Bug |
Priority | High |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2016-05-26T21:09:05.000+0000 |
Affected Version/s | Release 5.3.0 |
Fix Version/s | Release 5.4.0 |
Components | Android |
Labels | android, httpclient |
Reporter | Richard Lustemberg |
Assignee | Ashraf Abu |
Created | 2016-05-06T22:22:15.000+0000 |
Updated | 2016-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
Community PR: https://github.com/appcelerator/titanium_mobile/pull/7993 [~msamah] fyi
[~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.:-
I'm reviewing your PR and it looks good.
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.
[~rlustemberg] Code reviewed and merged. https://github.com/appcelerator/titanium_mobile/pull/7993 Thank you!
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.0Reopening to correct SDK version