Skip to content

Commit

Permalink
fix: allow single parameter on setRequestBody (#6728)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jul 13, 2023
1 parent 4fdbe6e commit 5343bdb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5343bdb

Please # to comment.