diff --git a/CHANGELOG.md b/CHANGELOG.md index f567ecb..3465e55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 2.4.1 - Fixed #296: multipart requests are not serialized on browser platform +- Fixed #301: data is not decoded correctly when responseType is "json" (thanks antikalk) ## 2.4.0 diff --git a/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java b/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java index a86c46f..277f8e7 100644 --- a/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java +++ b/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java @@ -193,7 +193,7 @@ abstract class CordovaHttpBase implements Runnable { response.setHeaders(request.headers()); if (request.code() >= 200 && request.code() < 300) { - if ("text".equals(this.responseType)) { + if ("text".equals(this.responseType) || "json".equals(this.responseType)) { String decoded = HttpBodyDecoder.decodeBody(outputStream.toByteArray(), request.charset()); response.setBody(decoded); } else { diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index ac559c6..2f8c750 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -62,7 +62,7 @@ } - (void)setResponseSerializer:(NSString*)responseType forManager:(AFHTTPSessionManager*)manager { - if ([responseType isEqualToString: @"text"]) { + if ([responseType isEqualToString: @"text"] || [responseType isEqualToString: @"json"]) { manager.responseSerializer = [TextResponseSerializer serializer]; } else { manager.responseSerializer = [BinaryResponseSerializer serializer]; diff --git a/test/e2e-specs.js b/test/e2e-specs.js index 4e1bcc9..36f4c7c 100644 --- a/test/e2e-specs.js +++ b/test/e2e-specs.js @@ -3,7 +3,7 @@ const hooks = { cordova.plugin.http.clearCookies(); helpers.enableFollowingRedirect(function() { - // server trust mode is not supported on brpwser platform + // server trust mode is not supported on browser platform if (cordova.platformId === 'browser') { return resolve(); } @@ -787,7 +787,7 @@ const tests = [ }, { description: 'should decode error body even if response type is "arraybuffer"', - expected: 'rejected: {"status": 418, ...', + expected: 'rejected: {"status":418, ...', func: function (resolve, reject) { var url = 'https://httpbin.org/status/418'; var options = { method: 'get', responseType: 'arraybuffer' }; @@ -801,7 +801,7 @@ const tests = [ }, { description: 'should serialize FormData instance correctly when it contains string value', - expected: 'resolved: {"status": 200, ...', + expected: 'resolved: {"status":200, ...', before: helpers.setMultipartSerializer, func: function (resolve, reject) { var ponyfills = cordova.plugin.http.ponyfills; @@ -820,7 +820,7 @@ const tests = [ }, { description: 'should serialize FormData instance correctly when it contains blob value', - expected: 'resolved: {"status": 200, ...', + expected: 'resolved: {"status":200, ...', before: helpers.setMultipartSerializer, func: function (resolve, reject) { var ponyfills = cordova.plugin.http.ponyfills; @@ -890,6 +890,39 @@ const tests = [ result.data.headers['access-control-allow-origin'].should.be.equal('*'); } }, + { + description: 'should decode JSON data correctly when response type is "json" #301', + expected: 'resolved: {"status":200,"data":{"slideshow": ... ', + func: function (resolve, reject) { + var url = 'https://httpbin.org/json'; + var options = { method: 'get', responseType: 'json' }; + cordova.plugin.http.sendRequest(url, options, resolve, reject); + }, + validationFunc: function (driver, result) { + result.type.should.be.equal('resolved'); + result.data.status.should.be.equal(200); + result.data.data.should.be.an('object'); + result.data.data.slideshow.should.be.eql({ + author: 'Yours Truly', + date: 'date of publication', + slides: [ + { + title: 'Wake up to WonderWidgets!', + type: 'all' + }, + { + items: [ + 'Why WonderWidgets are great', + 'Who buys WonderWidgets' + ], + title: 'Overview', + type: 'all' + } + ], + title: 'Sample Slide Show' + }); + } + }, // TODO: not ready yet // {