Skip to content

Unset Content-Type for FormData as request body before Request() constructor call #1550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/nasty-squids-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix 'Content-Type' header being removed from requests with multipart/form-data body
8 changes: 4 additions & 4 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ export default function createClient(clientOptions) {
if (requestInit.body) {
requestInit.body = bodySerializer(requestInit.body);
}
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
if (requestInit.body instanceof FormData) {
requestInit.headers.delete("Content-Type");
}
let request = new Request(
createFinalURL(url, { baseUrl, params, querySerializer }),
requestInit,
);
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
if (requestInit.body instanceof FormData) {
request.headers.delete("Content-Type");
}
// middleware (request)
const mergedOptions = {
baseUrl,
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,9 @@ describe("client", () => {
const req = fetchMocker.mock.calls[0][0];
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
expect(req.body).toBeInstanceOf(Buffer);

// 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
expect(req.headers.get("Content-Type")).toBeNull();
expect((req.headers as Headers).get("Content-Type")).toBe(
"text/plain;charset=UTF-8",
);
});

// Node Requests eat credentials (no cookies), but this works in frontend
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-fetch/test/v7-beta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,9 +946,9 @@ describe("client", () => {
const req = fetchMocker.mock.calls[0][0];
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
expect(req.body).toBeInstanceOf(Buffer);

// 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
expect((req.headers as Headers).get("Content-Type")).toBeNull();
expect((req.headers as Headers).get("Content-Type")).toBe(
"text/plain;charset=UTF-8",
);
});

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