Skip to content

Commit a5a9cc7

Browse files
authored
Unset Content-Type for FormData as request body before Request() constructor call (#1550)
* Unset Content-Type for FormData as request body before Request() const. call. Fixes #1548 content-type header with multipart boundary set by Request() is retained * Added changeset * Fix lint:prettier errors
1 parent 484ceb9 commit a5a9cc7

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

.changeset/nasty-squids-collect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Fix 'Content-Type' header being removed from requests with multipart/form-data body

packages/openapi-fetch/src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ export default function createClient(clientOptions) {
6565
if (requestInit.body) {
6666
requestInit.body = bodySerializer(requestInit.body);
6767
}
68+
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
69+
if (requestInit.body instanceof FormData) {
70+
requestInit.headers.delete("Content-Type");
71+
}
6872
let request = new Request(
6973
createFinalURL(url, { baseUrl, params, querySerializer }),
7074
requestInit,
7175
);
72-
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
73-
if (requestInit.body instanceof FormData) {
74-
request.headers.delete("Content-Type");
75-
}
7676
// middleware (request)
7777
const mergedOptions = {
7878
baseUrl,

packages/openapi-fetch/test/index.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -937,9 +937,9 @@ describe("client", () => {
937937
const req = fetchMocker.mock.calls[0][0];
938938
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
939939
expect(req.body).toBeInstanceOf(Buffer);
940-
941-
// TODO: `vitest-fetch-mock` does not add the boundary to the Content-Type header like browsers do, so we expect the header to be null instead
942-
expect(req.headers.get("Content-Type")).toBeNull();
940+
expect((req.headers as Headers).get("Content-Type")).toBe(
941+
"text/plain;charset=UTF-8",
942+
);
943943
});
944944

945945
// Node Requests eat credentials (no cookies), but this works in frontend

packages/openapi-fetch/test/v7-beta.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,9 @@ describe("client", () => {
946946
const req = fetchMocker.mock.calls[0][0];
947947
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
948948
expect(req.body).toBeInstanceOf(Buffer);
949-
950-
// TODO: `vitest-fetch-mock` does not add the boundary to the Content-Type header like browsers do, so we expect the header to be null instead
951-
expect((req.headers as Headers).get("Content-Type")).toBeNull();
949+
expect((req.headers as Headers).get("Content-Type")).toBe(
950+
"text/plain;charset=UTF-8",
951+
);
952952
});
953953

954954
// Node Requests eat credentials (no cookies), but this works in frontend

0 commit comments

Comments
 (0)