Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
fix(ios): null in body or result
Browse files Browse the repository at this point in the history
  • Loading branch information
Benni committed Feb 2, 2022
1 parent 3aca516 commit c2273e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 3 additions & 1 deletion ios/Plugin/CapacitorUrlRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate {

private func getRequestDataAsJson(_ data: JSValue) throws -> Data? {
// We need to check if the JSON is valid before attempting to serialize, as JSONSerialization.data will not throw an exception that can be caught, and will cause the application to crash if it fails.
if JSONSerialization.isValidJSONObject(data) {
if (data is NSNull) {
return "null".data(using: .utf8)
} else if JSONSerialization.isValidJSONObject(data) {
return try JSONSerialization.data(withJSONObject: data)
} else {
throw CapacitorUrlRequest.CapacitorUrlRequestError.serializationError("[ data ] argument for request of content-type [ application/json ] must be serializable to JSON")
Expand Down
14 changes: 9 additions & 5 deletions ios/Plugin/HttpRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ fileprivate enum ResponseType: String {
/// - data: The JSON Data to parse
/// - Returns: The parsed value or an error
func tryParseJson(_ data: Data) -> Any {
do {
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
} catch {
return error.localizedDescription
}
if data.elementsEqual("null".data(using: .utf8)!) {
return NSNull()
}

do {
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
} catch {
return error.localizedDescription
}
}

class HttpRequestHandler {
Expand Down

0 comments on commit c2273e9

Please sign in to comment.