Skip to content
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

feat: Adds support for application/x-www-form-urlencoded in Request.formData #1379

Merged
merged 2 commits into from
Apr 4, 2024

Conversation

tt-public
Copy link
Contributor

Currently Request.formData only handles "multipart/form-data", but it seems that it should also support "application/x-www-form-urlencoded" according to the specs. https://fetch.spec.whatwg.org/#dom-body-formdata

Current behavior:

const params = new URLSearchParams();
params.append("d", "data");

const req = new Request("https://example.com", { method: "POST", body: params });

for (const [key, value] of req.headers.entries()) {
    console.log(`${key}: ${value.split(";")[0]}`);
}
// content-type: application/x-www-form-urlencoded

(async () => {
    for (const [key, value] of (await req.formData()).entries()) {
        console.log(`${key}: ${value}`);
    }
    // DOMException [InvalidStateError]: Failed to build FormData object: The "content-type" header isn't of type "multipart/form-data".
})();

The current implementation of Request.formData in happy-dom directly calls a method which seems to be based on toFormData in node-fetch. [source] However, Request.formData in node-fetch only calls toFormData after handling "application/x-www-form-urlencoded" [source], which probably should be reflected in this project too.

Thank you for the awesome project. I hope this helps!

… Request.formData() and Response.formData() more in sync
Copy link
Owner

@capricorn86 capricorn86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing @tt-public! ⭐

I added some unit tests and synced the logic between Request.formData() and Response.formData().

I'm not sure why this was missed as Response.formData() had the logic for handling "application/x-www-form-urlencoded".

@capricorn86 capricorn86 merged commit 5e160ed into capricorn86:master Apr 4, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants