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

+server.ts API endpoint fails to parse body #7409

Closed
callmeberzerker opened this issue Oct 27, 2022 · 10 comments · Fixed by #7412
Closed

+server.ts API endpoint fails to parse body #7409

callmeberzerker opened this issue Oct 27, 2022 · 10 comments · Fixed by #7412
Labels
bug Something isn't working p0-urgent SvelteKit is broken or vulnerable for most users

Comments

@callmeberzerker
Copy link

Describe the bug

The only file new file in the blank svelte-kit project is the +server.ts located in src/routes/api/+server.ts. It just echoes the incoming JSON. Hitting http://localhost:5173/api (with content-type and accept headers set to application/json and body { "sample": "test"} ) will return HTTP 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:

Unexpected end of JSON input
SyntaxError: Unexpected end of JSON input

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 install and then pnpm dev.

Logs

Sadly there are none.

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.0

Severity

blocking all usage of SvelteKit

Additional Information

N/A

@georgdonner
Copy link

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"

@Rich-Harris
Copy link
Member

Oddly, 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 🤔

@callmeberzerker
Copy link
Author

Thanks for the response - I almost went crazy trying out different things for this seemingly straight-forward thing!

@Conduitry
Copy link
Member

I think this is due to the new version of undici that was just released a few hours ago. Its Request implementation is throwing RequestInit: duplex option is required when sending a body. for the incoming request.

@callmeberzerker callmeberzerker changed the title +server.ts API endpoint fails to parse body (possible Windows issue?) +server.ts API endpoint fails to parse body Oct 27, 2022
@Conduitry
Copy link
Member

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.

@Conduitry Conduitry added bug Something isn't working p0-urgent SvelteKit is broken or vulnerable for most users labels Oct 27, 2022
@Conduitry
Copy link
Member

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 Request now or if it looks like the new behavior is a bug.

@callmeberzerker
Copy link
Author

Yep, I can confirm pinning undici@5.11.0 via pnpm.overrides (https://pnpm.io/package_json#pnpmoverrides) fixed the issue on my project for the time being. Thanks! 🙌

@KhafraDev
Copy link

KhafraDev commented Oct 27, 2022

@careerlister
Copy link
Contributor

Is there a work around or will the patch be coming soon?

@Conduitry
Copy link
Member

Until #7412 is released, the workaround is to use overrides to force undici@5.11.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p0-urgent SvelteKit is broken or vulnerable for most users
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants