Skip to content

Commit

Permalink
fix: Let HttpNativeImpl make multipart/form-data w/ appropriate boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jun 18, 2022
1 parent 5536100 commit f277388
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/http/http-native-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export class HttpNativeImpl extends BaseHttp implements Http {
);
const contentType = headers.get('Content-Type') ?? 'application/json';
const body = this.serializer.serialize(contentType as MimeType, data);
if (
body instanceof FormData &&
contentType == 'multipart/form-data' &&
HttpNativeImpl.hasBlob(body)
) {
// As multipart form data should contain an arbitrary boundary,
// leave Content-Type header undefined, so that fetch() API
// automatically configure Content-Type with an appropriate boundary.
headers.delete('Content-Type');
}

try {
const response = await fetch(url, {
Expand Down Expand Up @@ -73,4 +83,10 @@ export class HttpNativeImpl extends BaseHttp implements Http {
});
return result;
}

private static hasBlob(formData: FormData): boolean {
let hasBlob = false;
formData.forEach((v: string | Blob) => (hasBlob ||= v instanceof Blob));
return hasBlob;
}
}

0 comments on commit f277388

Please sign in to comment.