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

Commit c2273e9

Browse files
author
Benni
committed
fix(ios): null in body or result
1 parent 3aca516 commit c2273e9

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

ios/Plugin/CapacitorUrlRequest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate {
2525

2626
private func getRequestDataAsJson(_ data: JSValue) throws -> Data? {
2727
// 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.
28-
if JSONSerialization.isValidJSONObject(data) {
28+
if (data is NSNull) {
29+
return "null".data(using: .utf8)
30+
} else if JSONSerialization.isValidJSONObject(data) {
2931
return try JSONSerialization.data(withJSONObject: data)
3032
} else {
3133
throw CapacitorUrlRequest.CapacitorUrlRequestError.serializationError("[ data ] argument for request of content-type [ application/json ] must be serializable to JSON")

ios/Plugin/HttpRequestHandler.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ fileprivate enum ResponseType: String {
3131
/// - data: The JSON Data to parse
3232
/// - Returns: The parsed value or an error
3333
func tryParseJson(_ data: Data) -> Any {
34-
do {
35-
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
36-
} catch {
37-
return error.localizedDescription
38-
}
34+
if data.elementsEqual("null".data(using: .utf8)!) {
35+
return NSNull()
36+
}
37+
38+
do {
39+
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
40+
} catch {
41+
return error.localizedDescription
42+
}
3943
}
4044

4145
class HttpRequestHandler {

0 commit comments

Comments
 (0)