Skip to content

Commit 3373929

Browse files
authored
fix: [#2106] Request.formData() should honor Content-Type header (#2107)
`Request.formData()` was checking the internal `PropertySymbol.contentType` (inferred from the body init) instead of the actual `Content-Type` header. This caused `formData()` to fail when a string body was paired with an explicit `Content-Type` header, since the body-inferred type was always `text/plain;charset=UTF-8`. This aligns `Request.formData()` with `Response.formData()`, which already reads from headers.
1 parent 55c17ba commit 3373929

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

packages/happy-dom/src/fetch/Request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ export default class Request implements Request {
432432
const window = this[PropertySymbol.window];
433433
const asyncTaskManager = new WindowBrowserContext(window).getAsyncTaskManager()!;
434434

435-
const contentType = this[PropertySymbol.contentType];
435+
const contentType = this.headers.get('Content-Type') ?? this[PropertySymbol.contentType];
436436

437437
if (this.body && contentType && /multipart/i.test(contentType)) {
438438
if (this[PropertySymbol.bodyUsed]) {

packages/happy-dom/test/fetch/Request.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,19 @@ describe('Request', () => {
729729
expect(formDataResponse.get('some')).toBe('test');
730730
});
731731

732+
it('Returns FormData for string body with explicit Content-Type header.', async () => {
733+
const request = new window.Request(TEST_URL, {
734+
method: 'POST',
735+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
736+
body: 'key1=value1&key2=value2&key3=value3'
737+
});
738+
const formDataResponse = await request.formData();
739+
740+
expect(formDataResponse.get('key1')).toBe('value1');
741+
expect(formDataResponse.get('key2')).toBe('value2');
742+
expect(formDataResponse.get('key3')).toBe('value3');
743+
});
744+
732745
it('Returns FormData for "application/x-www-form-urlencoded" content.', async () => {
733746
const urlSearchParams = new URLSearchParams();
734747

0 commit comments

Comments
 (0)