Skip to content

Commit 6877fbd

Browse files
author
Sefa Ilkimen
committed
fix silkimen#78: override header "Content-Type" correctly on Android
1 parent 3f1ee62 commit 6877fbd

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.10.2
4+
5+
-Fixed #78: overriding header "Content-Type" not working on Android
6+
37
## 1.10.1
48

59
- Fixed #71: does not encode query string in URL correctly on Android

src/android/com/synconset/cordovahttp/CordovaHttp.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,12 @@ protected HttpRequest setupRedirect(HttpRequest request) {
149149
return request;
150150
}
151151

152-
protected HttpRequest setupDataSerializer(HttpRequest request) throws JSONException, Exception {
152+
protected void setupDataSerializer(HttpRequest request) throws JSONException, Exception {
153153
if ("json".equals(this.getSerializerName())) {
154154
request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8);
155-
request.send(this.getParamsObject().toString());
156155
} else if ("utf8".equals(this.getSerializerName())) {
157156
request.contentType("text/plain", request.CHARSET_UTF8);
158-
request.send(this.getParamsMap().get("text").toString());
159-
} else {
160-
request.form(this.getParamsMap());
161157
}
162-
163-
return request;
164158
}
165159

166160
protected void respondWithError(int status, String msg) {
@@ -240,6 +234,16 @@ protected void prepareRequest(HttpRequest request) throws HttpRequestException,
240234
request.uncompress(true);
241235
}
242236

237+
protected void prepareRequestBody(HttpRequest request) throws JSONException, Exception {
238+
if ("json".equals(this.getSerializerName())) {
239+
request.send(this.getParamsObject().toString());
240+
} else if ("utf8".equals(this.getSerializerName())) {
241+
request.send(this.getParamsMap().get("text").toString());
242+
} else {
243+
request.form(this.getParamsMap());
244+
}
245+
}
246+
243247
private CharsetDecoder createCharsetDecoder(final String charsetName) {
244248
return Charset.forName(charsetName).newDecoder()
245249
.onMalformedInput(CodingErrorAction.REPORT)

src/android/com/synconset/cordovahttp/CordovaHttpPatch.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public void run() {
2525
try {
2626
HttpRequest request = HttpRequest.patch(this.getUrlString());
2727

28-
this.prepareRequest(request);
2928
this.setupDataSerializer(request);
29+
this.prepareRequest(request);
30+
this.prepareRequestBody(request);
3031
this.returnResponseObject(request);
3132
} catch (HttpRequestException e) {
3233
this.handleHttpRequestException(e);

src/android/com/synconset/cordovahttp/CordovaHttpPost.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public void run() {
2525
try {
2626
HttpRequest request = HttpRequest.post(this.getUrlString());
2727

28-
this.prepareRequest(request);
2928
this.setupDataSerializer(request);
29+
this.prepareRequest(request);
30+
this.prepareRequestBody(request);
3031
this.returnResponseObject(request);
3132
} catch (HttpRequestException e) {
3233
this.handleHttpRequestException(e);

src/android/com/synconset/cordovahttp/CordovaHttpPut.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public void run() {
2525
try {
2626
HttpRequest request = HttpRequest.put(this.getUrlString());
2727

28-
this.prepareRequest(request);
2928
this.setupDataSerializer(request);
29+
this.prepareRequest(request);
30+
this.prepareRequestBody(request);
3031
this.returnResponseObject(request);
3132
} catch (HttpRequestException e) {
3233
this.handleHttpRequestException(e);

test/app-test-definitions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,15 @@ const tests = [
446446
result.type.should.be.equal('resolved');
447447
JSON.parse(result.data.data).json.should.eql({ outerObj: { innerStr: 'testString', innerArr: [1, 2, 3] }});
448448
}
449+
},{
450+
description: 'should override header "content-type" correctly (POST) #78',
451+
expected: 'resolved: {"status": 200, "headers": "{\\"Content-Type\\": \\"text/plain\\" ...',
452+
before: helpers.setJsonSerializer,
453+
func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', {}, { 'Content-Type': 'text/plain' }, resolve, reject); },
454+
validationFunc: function(driver, result) {
455+
result.type.should.be.equal('resolved');
456+
JSON.parse(result.data.data).headers['Content-Type'].should.be.equal('text/plain');
457+
}
449458
}
450459
];
451460

0 commit comments

Comments
 (0)