Skip to content

Commit 54140de

Browse files
committed
fix: support ArrayBuffer/Blob for form data
1 parent 79739fa commit 54140de

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/https/request.android.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,19 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
519519
if (param.data instanceof okhttp3.RequestBody) {
520520
builder.addFormDataPart(param.parameterName, param.fileName, param.data);
521521
} else {
522-
builder.addFormDataPart(param.parameterName, param.fileName, okhttp3.RequestBody.create(param.data, okhttp3.MediaType.parse(param.contentType)));
522+
let nData = param.data;
523+
if (param.data instanceof ArrayBuffer) {
524+
const typedArray = new Uint8Array(param.data);
525+
const nativeBuffer = java.nio.ByteBuffer.wrap(Array.from(typedArray));
526+
nData = nativeBuffer.array();
527+
} else if (param.data instanceof Blob) {
528+
// Stolen from core xhr, not sure if we should use InternalAccessor, but it provides fast access.
529+
// @ts-ignore
530+
const typedArray = new Uint8Array(Blob.InternalAccessor.getBuffer(param.data).buffer.slice(0) as ArrayBuffer);
531+
const nativeBuffer = java.nio.ByteBuffer.wrap(Array.from(typedArray));
532+
nData = nativeBuffer.array();
533+
}
534+
builder.addFormDataPart(param.parameterName, param.fileName, okhttp3.RequestBody.create(nData, okhttp3.MediaType.parse(param.contentType)));
523535
}
524536
} else {
525537
if (typeof param.data === 'string') {

src/https/request.ios.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,16 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
464464
let data = param.data;
465465
if (typeof data === 'string') {
466466
data = NSString.stringWithString(data).dataUsingEncoding(NSUTF8StringEncoding);
467+
} else if (data instanceof ArrayBuffer) {
468+
const buffer = new Uint8Array(data);
469+
data = NSData.dataWithData(buffer as any);
470+
} else if (data instanceof Blob) {
471+
// Stolen from core xhr, not sure if we should use InternalAccessor, but it provides fast access.
472+
// @ts-ignore
473+
const buffer = new Uint8Array(Blob.InternalAccessor.getBuffer(data).buffer.slice(0) as ArrayBuffer);
474+
data = NSData.dataWithData(buffer as any);
467475
}
476+
468477
formData.appendPartWithFileDataNameFileNameMimeType(data, param.parameterName, param.fileName, param.contentType);
469478
}
470479
} else {

0 commit comments

Comments
 (0)