Skip to content

Commit b033c33

Browse files
committed
Unset Content-Type for FormData as request body before Request() const. call. Fixes openapi-ts#1548
content-type header with multipart boundary set by Request() is retained
1 parent 484ceb9 commit b033c33

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -937,9 +937,7 @@ 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("text/plain;charset=UTF-8");
943941
});
944942

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

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,7 @@ 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("text/plain;charset=UTF-8");
952950
});
953951

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

0 commit comments

Comments
 (0)