diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java index 32e7502d8..35ab35e09 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java @@ -169,6 +169,16 @@ public void setDoOutput(boolean shouldDoOutput) { connection.setDoOutput(shouldDoOutput); } + /** + * + * @param call + * @throws JSONException + * @throws IOException + */ + public void setRequestBody(PluginCall call, JSValue body) throws JSONException, IOException { + setRequestBody(call, body, null); + } + /** * * @param call @@ -194,14 +204,14 @@ public void setRequestBody(PluginCall call, JSValue body, String bodyType) throw dataString = call.getString("data"); } this.writeRequestBody(dataString != null ? dataString : ""); - } else if (bodyType.equals("file")) { + } else if (bodyType != null && bodyType.equals("file")) { try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { os.write(Base64.getDecoder().decode(body.toString())); } os.flush(); } - } else if (bodyType.equals("formData")) { + } else if (bodyType != null && bodyType.equals("formData")) { this.writeFormDataRequestBody(contentType, body.toJSArray()); } else { this.writeRequestBody(body.toString()); diff --git a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift index 301fe2e90..51331e3be 100644 --- a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift +++ b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift @@ -144,7 +144,7 @@ open class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { return data } - public func getRequestData(_ body: JSValue, _ contentType: String, _ dataType: String) throws -> Data? { + public func getRequestData(_ body: JSValue, _ contentType: String, _ dataType: String? = nil) throws -> Data? { if dataType == "file" { guard let stringData = body as? String else { throw CapacitorUrlRequestError.serializationError("[ data ] argument could not be parsed as string") @@ -185,7 +185,7 @@ open class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { } } - public func setRequestBody(_ body: JSValue, _ dataType: String) throws { + public func setRequestBody(_ body: JSValue, _ dataType: String? = nil) throws { let contentType = self.getRequestHeader("Content-Type") as? String if contentType != nil {