bun icon indicating copy to clipboard operation
bun copied to clipboard

FormData missing final boundary

Open yousefelgoharyx opened this issue 2 years ago • 13 comments

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

yousefelgoharyx avatar Apr 13 '23 10:04 yousefelgoharyx

With Astrobuild, i have this error :

TypeError: FormData parse error missing final boundary

Same thing is i submit with Json

Liliandev avatar Sep 25 '23 11:09 Liliandev

Same error with SvelteKit and Superform

lafkpages avatar Nov 17 '23 15:11 lafkpages

Can confirm the error on Sveltekit

nanolikestea avatar Nov 18 '23 10:11 nanolikestea

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

maxa-ondrej avatar Nov 23 '23 14:11 maxa-ondrej

Same error here

Dan1ve avatar Dec 05 '23 19:12 Dan1ve

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

seh-GAH-toh avatar Jan 19 '24 23:01 seh-GAH-toh

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)

yaikohi avatar Feb 13 '24 09:02 yaikohi

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

znycheporuk avatar Mar 09 '24 18:03 znycheporuk

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://github.com/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.

KyleFontenot avatar Mar 27 '24 19:03 KyleFontenot

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

ElYaiko avatar May 03 '24 21:05 ElYaiko

I'm able to reproduce this

Here is the repo you can clone to reproduce the error (SolidStart) https://github.com/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.

Electroid avatar May 03 '24 23:05 Electroid

I'm able to reproduce this

Here is the repo you can clone to reproduce the error (SolidStart) https://github.com/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.

ElYaiko avatar May 04 '24 00:05 ElYaiko

please fix it

zoto-ff avatar May 09 '24 22:05 zoto-ff

bump

ghost avatar May 21 '24 20:05 ghost

same error :/

aralroca avatar May 28 '24 21:05 aralroca

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.

karemont avatar Jun 09 '24 19:06 karemont

also present in astro

huseeiin avatar Aug 09 '24 20:08 huseeiin

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

Happens with Astro actions.

gtrabanco avatar Aug 12 '24 18:08 gtrabanco