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

FormData missing final boundary #2644

Closed
yousefelgoharyx opened this issue Apr 13, 2023 · 17 comments · Fixed by #2648 or #13744
Closed

FormData missing final boundary #2644

yousefelgoharyx opened this issue Apr 13, 2023 · 17 comments · Fixed by #2648 or #13744
Labels
bug Something isn't working web-api Something that relates to a standard Web API

Comments

@yousefelgoharyx
Copy link

What version of Bun is running?

0.5.9

What platform is your computer?

WSL - x64

What steps can reproduce the bug?

async function handler(req: Request) {
	const contentType = req.headers.get("Content-Type")
	if (!contentType) throw new Error("Missing Content-Type");

	if (contentType.startsWith("multipart/form-data")) {
		const fd = await req.formData()
		const body = Object.fromEntries(fd.entries());
		return new Response(JSON.stringify(body))
	}

	throw new Error("Invalid Content-Type");
}
Bun.serve({
	port: 3000,
	async fetch(request) {
		await new Promise((resolve) => setTimeout(resolve, 0));
		const response = await handler(request)
		await new Promise((resolve) => setTimeout(resolve, 0));
		return response;
	}
})

Here is the code causing the bug. Try to hit localhost:3000 many times quickly and the server will throw the following error message:
error: FormData missing final boundary

When removing the setTimeouts everything works correctly. That is what causing the problem.

What is the expected behavior?

Not to throw an error

What do you see instead?

The error described above.

Additional information

No response

@Liliandev
Copy link

With Astrobuild, i have this error :

TypeError: FormData parse error missing final boundary

Same thing is i submit with Json

@Electroid Electroid reopened this Sep 25, 2023
@Electroid Electroid added the web-api Something that relates to a standard Web API label Sep 25, 2023
@lafkpages
Copy link
Contributor

Same error with SvelteKit and Superform

@neebooo
Copy link

neebooo commented Nov 18, 2023

Can confirm the error on Sveltekit

@maxa-ondrej
Copy link

Getting the same error on SvelteKit with svelte-adapter-bun adapter.

@Dan1ve
Copy link

Dan1ve commented Dec 5, 2023

Same error here

@seh-GAH-toh
Copy link

Can confirm the error on Qwik with bun adapter.

Ubuntu - 23.10
Bun - 1.0.23
Qwik - 1.4.0
Qwik City - 1.4.0

@yaikohi
Copy link

yaikohi commented Feb 13, 2024

I am not sure if it is bun or H3, but I am receiving this error as well when using solidstart, which proxies h3s functions.

[h3] [unhandled] 1 | export default "native";
    ^
error: FormData parse error missing final boundary
      at formData (native:1:1)

1 | export default "native";
    ^
TypeError: FormData parse error missing final boundary
      at formData (native:1:1)

@znycheporuk
Copy link

Here is the repo you can clone to reproduce the error (SolidStart)
https:/znycheporuk/bun-formdata

@KyleFontenot
Copy link

KyleFontenot commented Mar 27, 2024

This makes Bun completely unusable for my purposes. After 1 year of the OP, and a 1.0 release, can we maybe bump up this priority? I get errors even when using Json like lilliandev said.

I'm trying to use a Sveltekit adapter (https:/gornostay25/svelte-adapter-bun) and essentially any endpoint does not receive a body at all, whether formdata, json, or text. Would raise the issue with the adapter repo if I didn't see so many other people encounter the same thing with other tools like SolidStart.

@ElYaiko
Copy link

ElYaiko commented May 3, 2024

Any update? It doesn't append the Boundary neither with fetch.
Not even node-fetch works.

@Electroid
Copy link
Contributor

Electroid commented May 3, 2024

I'm able to reproduce this

Here is the repo you can clone to reproduce the error (SolidStart) https:/znycheporuk/bun-formdata

This is reproducible, though getting a more minimal reproduction proved difficult.

It looks like there is some issue with how solidstart's dependencies are cloning the request and its body that causes the body to be empty by the time .formData() is called. We will still fix this, but it's more complicated.

@ElYaiko
Copy link

ElYaiko commented May 4, 2024

I'm able to reproduce this

Here is the repo you can clone to reproduce the error (SolidStart) https:/znycheporuk/bun-formdata

This is reproducible, though getting a more minimal reproduction proved difficult.

It looks like there is some issue with how solidstart's dependencies are cloning the request and its body that causes the body to be empty by the time .formData() is called. We will still fix this, but it's more complicated.

To me happens even without solidstart, just sending a simple request with a FormData body doesn't attach the Boundary so the servers doesn't detect the multipart/form-data request.

@ghost
Copy link

ghost commented May 21, 2024

bump

@aralroca
Copy link
Contributor

same error :/

@karemont
Copy link

karemont commented Jun 9, 2024

I had this issue on my class

class Parent {
	public async fetch(req: Request) {
		const httpRequest = new HTTPRequest(req)
		console.log(await httpRequest.formData())
	}
}

in which HTTPRequest is just a wrapper

class HTTPRequest extends Request {
    message = 'Hello'
    constructor(input: RequestInfo, init?: RequestInit) {
        super(input, init);
    }
}

I resolved it using a copy function inside the parent class

class Parent {
	private copy(req: Request) {
		const r = req as HTTPRequest
		r.message = 'Hello'
		return r
	}
	public async fetch(req: Request) {
		const httpRequest = this.copy(req)
		console.log(await httpRequest.formData());
	}
}

hopefully this helps solve this bug, somehow, clearly it is related to cloning the request.

@huseeiin
Copy link
Contributor

huseeiin commented Aug 9, 2024

also present in astro

@gtrabanco
Copy link

Bun: 1.1.23-canary.30+444766833
Astro: 4.13.3
Platform: MacOS 15.0 M3

Happens with Astro actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working web-api Something that relates to a standard Web API
Projects
None yet