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

Feat: Support multipart form data with same attribute name #223

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
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ public void addFilePart(String fieldName, File uploadFile, JSObject data) throws
try {
Object value = data.get(key);

if (!(value instanceof String)) continue;

appendFieldToWriter(key, value.toString());
if (value instanceof String) {
appendFieldToWriter(key, value.toString());
} else if (value instanceof String[]) {
for (childValue : value) {
appendFieldToWriter(key, childValue.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
19 changes: 12 additions & 7 deletions ios/Plugin/HttpRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ class HttpRequestHandler {
}

private static func generateMultipartForm(_ url: URL, _ name: String, _ boundary: String, _ body: [String:Any]) throws -> Data {
let strings: [String: String] = body.compactMapValues { any in
any as? String
}

var data = Data()

Expand All @@ -139,10 +136,18 @@ class HttpRequestHandler {
using: .utf8)!)
data.append("Content-Type: \(mimeType)\r\n\r\n".data(using: .utf8)!)
data.append(fileData)
strings.forEach { key, value in
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8)!)
data.append(value.data(using: .utf8)!)
body.forEach { key, value in
if let stringArray = value as? [String] {
for item in stringArray {
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8)!)
data.append(item.data(using: .utf8)!)
}
} else {
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8)!)
data.append((value as! String).data(using: .utf8)!)
}
}
data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)

Expand Down