Skip to content

Commit

Permalink
Fix parsing non-object JSON response in CapacitorHttp plugin (#6224)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Anderson <[email protected]>
  • Loading branch information
JanMisker and markemer authored Mar 22, 2023
1 parent fd225e5 commit ed47333
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ var nativeBridge = (function (exports) {
data: (options === null || options === void 0 ? void 0 : options.body) ? options.body : undefined,
headers: headers,
});
let data = typeof nativeResponse.data === 'string'
let data = !nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
// use null data for 204 No Content HTTP response
Expand Down Expand Up @@ -526,7 +526,7 @@ var nativeBridge = (function (exports) {
this.status = nativeResponse.status;
this.response = nativeResponse.data;
this.responseText =
typeof nativeResponse.data === 'string'
!nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
this.responseURL = nativeResponse.url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ public static Object parseJSON(String input) throws JSONException {
return new JSONObject().put("flag", "false");
} else if (input.trim().length() <= 0) {
return "";
} else if (input.trim().matches("^\".*\"$")) {
// a string enclosed in " " is a json value, return the string without the quotes
return input.trim().substring(1, input.trim().length() - 1);
} else {
try {
return new JSObject(input);
Expand Down
18 changes: 10 additions & 8 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,11 @@ const initBridge = (w: any): void => {
},
);

let data =
typeof nativeResponse.data === 'string'
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
let data = !nativeResponse.headers['Content-Type'].startsWith(
'application/json',
)
? nativeResponse.data
: JSON.stringify(nativeResponse.data);

// use null data for 204 No Content HTTP response
if (nativeResponse.status === 204) {
Expand Down Expand Up @@ -612,10 +613,11 @@ const initBridge = (w: any): void => {
this._headers = nativeResponse.headers;
this.status = nativeResponse.status;
this.response = nativeResponse.data;
this.responseText =
typeof nativeResponse.data === 'string'
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
this.responseText = !nativeResponse.headers[
'Content-Type'
].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
this.responseURL = nativeResponse.url;
this.readyState = 4;
this.dispatchEvent(new Event('load'));
Expand Down
2 changes: 1 addition & 1 deletion ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum ResponseType: String {
/// - Returns: The parsed value or an error
func tryParseJson(_ data: Data) -> Any {
do {
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
return try JSONSerialization.jsonObject(with: data, options: [.mutableContainers, .fragmentsAllowed])
} catch {
return error.localizedDescription
}
Expand Down
4 changes: 2 additions & 2 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ var nativeBridge = (function (exports) {
data: (options === null || options === void 0 ? void 0 : options.body) ? options.body : undefined,
headers: headers,
});
let data = typeof nativeResponse.data === 'string'
let data = !nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
// use null data for 204 No Content HTTP response
Expand Down Expand Up @@ -526,7 +526,7 @@ var nativeBridge = (function (exports) {
this.status = nativeResponse.status;
this.response = nativeResponse.data;
this.responseText =
typeof nativeResponse.data === 'string'
!nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
this.responseURL = nativeResponse.url;
Expand Down

0 comments on commit ed47333

Please sign in to comment.