Repository navigation
+server.ts API endpoint fails to parse body #7409
Description
Activity
I ran into the exact same issue today setting up a new Sveltekit project and am on MacOS, so it's probably not a Windows issue. It might be related to #5370, because I am seeing the same error "Invalid request body"
Reacted by Spase Markovski and Nikolas PoklitarOddly, I can reproduce this by cloning the repo, but as soon as I add a pnpm override to start debugging it, it works... a true Heisenbug 🤔
Reacted by Spase MarkovskiReacted by Spase MarkovskiThanks for the response - I almost went crazy trying out different things for this seemingly straight-forward thing!
I think this is due to the new version of undici that was just released a few hours ago. Its
Requestimplementation is throwingRequestInit: duplex option is required when sending a body.for the incoming request.Reacted by Spase Markovski and Mohammad Banaei- changed the title
[-]+server.ts API endpoint fails to parse body (possible Windows issue?)[/-][+]+server.ts API endpoint fails to parse body[/+]on Oct 27, 2022 Yeah, when I override to 5.11.0, things seem to work fine. The obvious change in 5.12.0 that would be causing this is nodejs/undici#1681 but I haven't dug any further into what this was intended to do or whether this is a new bug there or what.
Reacted by Spase Markovski, Mike Allen, Adrien, Hyunbin and Adnan Smajlovic- addedbugSomething isn't workingSomething isn't workingp0-urgentSvelteKit is broken or vulnerable for most usersSvelteKit is broken or vulnerable for most users
on Oct 27, 2022 I'm marking this as urgent. It might be a good idea to pin to undici 5.11.0 if we can't immediately see how we should now be calling
Requestnow or if it looks like the new behavior is a bug.Reacted by Spase Markovski and AdrienYep, I can confirm pinning
undici@5.11.0viapnpm.overrides(https://pnpm.io/package_json#pnpmoverrides) fixed the issue on my project for the time being. Thanks! 🙌Reacted by e0 and Craig HowellThis change is expected, see
whatwg/fetch@edf07e5#diff-b4827ee42aceaf9dc3e7216f72262d81c32217e9996fb70a657f259e3ac92604R6898-R6899Is there a work around or will the patch be coming soon?
Until #7412 is released, the workaround is to use overrides to force undici@5.11.0.
Reacted by Nikolas Poklitar, Spase Markovski, Sibbe and Adnan Smajlovic
Describe the bug
The only file new file in the blank svelte-kit project is the
+server.tslocated insrc/routes/api/+server.ts. It just echoes the incoming JSON. Hittinghttp://localhost:5173/api(withcontent-typeandacceptheaders set toapplication/jsonand body{ "sample": "test"}) will returnHTTP 400 Bad request-> and the request never hits the API endpoint.If I remove the content-type and accept headers it will enter the API endpoint but it would error out on
await request.json()with:It might be a Windows issue but honestly I am not sure and can't test atm.
Reproduction
https://github.com/callmeberzerker/body-not-working
Run
pnpm installand thenpnpm dev.Logs
System Info
System: OS: Windows 10 10.0.22621 CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz Memory: 16.30 GB / 31.92 GB Binaries: Node: 19.0.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Spartan (44.22621.674.0), Chromium (105.0.1343.42) Internet Explorer: 11.0.22621.1 npmPackages: vite: ^3.2.0 => 3.2.0Severity
blocking all usage of SvelteKit
Additional Information
N/A