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

fix(ios): null in body or result #228

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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