Skip to content

Form submit does not honour enctype #1774

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

Open
maxmil opened this issue Mar 16, 2025 · 1 comment
Open

Form submit does not honour enctype #1774

maxmil opened this issue Mar 16, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@maxmil
Copy link
Contributor

maxmil commented Mar 16, 2025

Describe the bug
When a form is submitted, if the form's method is not get the form data is always encoded as multipart form data.

To Reproduce

const browser = new Browser({
  settings: {
    fetch: {
      interceptor: {
        beforeAsyncRequest: async ({ request, window }) => {
          return new window.Response(await request.text());
        },
      },
    },
  },
});

const page = browser.newPage();
page.url = 'https://example.com';
page.content = `
  <html>
    <body>
      <form action="#" method="post">
        <input name="field1" type="text" value="foo">
        <input name="field2" type="text" value="bar">
      </form>
    </body>
  </html>`;

const form = page.mainFrame.document.body.querySelector('form');
form?.submit();
await page.waitForNavigation();

console.log(page.mainFrame.document.body.innerHTML);

Output:

------HappyDOMFormDataBoundary0.h8oieomkqnh
Content-Disposition: form-data; name="field1"

foo
------HappyDOMFormDataBoundary0.h8oieomkqnh
Content-Disposition: form-data; name="field2"

bar
------HappyDOMFormDataBoundary0.h8oieomkqnh--

Expected behavior
Since the default encoding for the form element is application/x-www-form-urlencoded we would expect the form data to be url encoded.

Additional context
I've had a quick look at the sources and think I could create a PR for this. I just want to check first that there is not a reason for the current behaviour. Also, I'm new to Happy DOM so I may be missing a trick.

@maxmil maxmil added the bug Something isn't working label Mar 16, 2025
@Spixmaster
Copy link

I hope this is related to #1761.

rslabbert added a commit to cubiko/happy-dom that referenced this issue Apr 2, 2025
Forms now correctly handle `<form enctype="...">`, `<button formenctype="...">`, and `<input type="button" formenctype="...">`.

Limitations:
1. `text/plain` is not supported.
2. `<input type="image">` is not supported.

BREAKING CHANGE: The default encoding for form submissions is now `application/x-www-form-urlencoded` instead of `multipart/form-data`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants